Zenity.rb

May 12th, 2009

Zenity is a command line based tool that helps shell scripts display GTK dialog boxes as a more interactive way to display error messages, get user input and other stuff like that. Read the rest of this entry »

Getting a Method to Accept an Arbitrary Number of Arguments in Ruby

May 9th, 2009

Read the rest of this entry »

Very Simple Metronome in Ruby

April 23rd, 2009

A metronome is basically something that beeps periodically, indicating the exact tempo of some music. I wanted one for my guitar practice, but the only one in Ubuntu’s repos (GTick) wouldn’t work, so I wrote this up. Read the rest of this entry »

How to Learn a New Programming Language

April 4th, 2009

Learning a new language isn’t the easiest thing to do. It can, however, be a very rewarding experience if you are persistent enough. Luckily, if you know how to code in one language, it is much, much easier to learn another language. Here are some tips that can help you. Read the rest of this entry »

Implemeting HQ9+ in 10 lines of Ruby

April 3rd, 2009

HQ9+ is a excessively simple esoteric programming language. It has only four commands:
H: Print “Hello, World!”
Q: Prints the contents of the program onto the screen
9: Print the canonical lyrics of the song, “99 bottles of beer”.
+: Increment the accumulator

Here’s the entire implementation:

1
2
3
4
5
6
7
8
9
10
def hq9(src)
  src.downcase.split('').each do |i|
    case i
      when 'h'; puts "Hello, World!"
      when 'q'; puts src
      when '9'; 99.downto(0) { |b| puts "#{b==0?'No more b':"#{b} B"}ottle#{'s'if b!=1} of beer on the wall, #{b==0?'No more':b} bottle#{'s'if b!=1} of beer.#{b==0?"nGo to the store and buy some more, 99 bottles of beer on the wall.":"nTake one down and pass it around, #{b-1} bottle#{'s'if b!=2} of beer on the wall.nn"}" }
      when '+'; $i = $i ? $i + 1 : 0;
    end
  end
end

Note: I got the lyric printing code from here.

Project Euler #48 in Ruby

April 2nd, 2009

Note: Don’t look at the code if you intend to solve the problem later.

Problem 48 – Project Euler

I was surprised at how easy this turned out to be. It is really, really simple because with Ruby, the only limit on how big an integer can get is how much memory you have available. Here’s my solution:

# Project Euler, 48
start = Time.now
 
n = 1
sum = 0
 
1000.times do
  sum += n**n
  n += 1
end
 
puts sum.to_s[-10..-1]
puts "Time taken: #{Time.now-start}"

It takes a fifth of a second on my laptop running Ruby 1.9.1.

Ruby Style Blocks in Javascript

February 23rd, 2009

Javascript is a really great language when it comes to flexibility. It is possible to pass code to a function the way you can do with Ruby, albeit a bit more verbose. This is made possible by the fact that javascript allows you to pass a function as one of the parameters of a function. Read the rest of this entry »

Compiling Ruby 1.9.1 (stable) on Ubuntu

February 18th, 2009

I found the default ruby install that came with Ubuntu to be a bit old, so I decided to reinstall it. And I like to use the newest version, always. Read the rest of this entry »

Pseudo-Classes in Lua

February 4th, 2009

Lua doesn’t have classes built in, but they can be simulated using tables. Let’s see how we can do this by attempting to create a math library which will contain some basic functions and values. Read the rest of this entry »

Introduction to Lua

February 3rd, 2009

Lua is a very elegant and powerful language. It is designed to be used as an extension language, but you can also use it by itself. It is very lightweight and extensible, and has a very simple and elegant C API. It is very popular in the video gaming industry as an extension language. Read the rest of this entry »