Ruby

I'm bored, let's learn a new programming language: Ruby.

Ruby is very much the new kid on the block of programming languages, despite having been around since '95. Lately it's picking up a bit of speed in the web development area, thanks to the Ruby on Rails framework. But enough background, let's get started with Hello World.

First you need to install ruby, of course. As usual, I'm going to proceed assuming you're using a UNIX variant operating system. The code itself will run on any system, but you need to replace the first line on other systems to get it to run.

#!/usr/bin/env ruby puts "Hello world, it's Christmas!"

There's little worth noting here. The Ruby method for printing is puts. Ruby methods do usually have the old familiar parenthesis syntax, but by means of syntactic sugar they can be omitted with puts. One thing worth noting is that we didn't append our string with \n, making puts similar in functionality to System.out.println() that we saw in the java lesson. The other thing that's different this time isn't strictly Ruby related, but rather a portability trick. Instead of telling the terminal exactly where ruby is installed on the system, we let /usr/bin/env figure out which ruby to run. Different OS's put it in different locations, and this gets around that.

Make it do something funny!

It's the holiday season, so let's act like it. How about pulling some bottles of beer off the wall? #!/usr/bin/env ruby def bottles?(arg) if arg==1 then return "#{arg} bottle" else return "#{arg} bottles" end end 99.downto(1){ |n| puts print "#{bottles?(n)} of beer on the wall, " puts "#{bottles?(n)} of beer." print "Take one down, pass it around, " puts "#{bottles?(n - 1)} of beer on the wall." }

Mkaaay, there's some stuff here that might look unfamiliar, and there's some stuff here that you should pick up right away now. I'll make a list.

  • print: Yes, there's a print in this language. puts newlineterminates your string, print does not. Next.
  • def bottles?(arg): Don't be fooled by the question mark, this is just your regular function declaration. It's only valid at the end of function names, and is usually used to show that a function returns true or false. I cheated to show it to you, and because it makes sense here.
  • if arg==1 then [code] end: Ruby is one language in a large family that frowns upon your squiggly abuse. Squigglies are used in Ruby, but then - end, do - end, and def func - end, are more prominent. It's a bit different from the other languages we've done, but you will see similar approaches in later languages, so it's best to get used to it.
  • "#{arg} bottle": Well, this is fun. This is how you embed a variable in a string, and also the only way to concatenate a string with, say, a number. Anything inside #{} is interpreted as code, as you can see when we embed a function. This style of allowing for dynamic content without leaving the string is popular in scripting languages, such as php.
  • 99.downto(1){: Well, there's your squiggly bracket. Ruby has a while function (while bottles > 1 do [code] end, but it also has these nifty downto and upto functions. Their names are quite descriptive, and aid code readability. Also 99 is not an integer primitive, it's an Integer instance, an object with all the associated functions.

What's that ugly |n| bugger doing there?

This was the feature that surprised and baffled me the most with Ruby. Allow me to introduce you to the concept of binding. 99.downto(1){ |n| puts n } It's much easier to understand what binding does than how it does it. 99.downto(1) iterates downward, and for every iteration it binds the current value to the variable n, that's what |variable| means: Bind to variable. a = ['P', 'l', 'e', 'a', 's', 'e', ' ', 'h', 'e', 'l', 'p', ' ', 'm', 'e', ',', ' ', 'O', 'h', ' ', 'G', 'o', 'd', ' ', 'I', '\'', 'm', ' ', 's', 'o', ' ', 'b', 'o', 'r', 'e', 'd', '!'] a.each do |char| print char end print "\n" Here we've implemented our very own puts method. As we remember from previous lessons, a String is just a collection of characters. Here we iterate over each character, assign its value to the variable char, and print it.

And that's all the time we had for today! Now go make something cool in Ruby, and come back next week. I rather like this language, so I think we'll be sticking to it for a while and making more cool stuff.


Song of the blog: Depui - King
Happy Saturnalia people!
Bjørn

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *