Ruby - “Oh No He Didn’t Just Do That”

I am sitting at my computer and I have an idea. I want to print out the square of each number from 1 to 200 because I think that it might be useful in solving a problem that I am facing. I happen to know Ruby, and while I was thinking it, I typing this in my terminal:

ruby -e "1.upto(200) { |i| puts i**2 }" > squares.txt

No wait… I typed this:

ruby -e "(1..200).each { |i| puts i*i }" > squares.txt

Perfect, the output went to a file named “squares.txt” where I can easily access it again and again. What were you doing? Was it something like this:

public class Squares {
  public static void main(String[] args) {
    for (int i = 0; i <= 200; i++) {
      System.out.println(i*i);
    }
  }
}

Not to mention the time to compile it… the output did not even go to a file for easy reading in the future. Your counter argument was that my Ruby solution didn’t either? Let me do it again, only this time I won’t do a terminal one liner (even though its not complex), but I’ll write you a full ruby script:

File.open('squares.txt','w') do |f|
  1.upto(200) { |i| f.puts i**2 }
end

Can you imagine what writing to a file would look like in Java? I can’t remember what to import, which writer I should wrap in a buffer, and oh my goodness lets not forget about the try catch block if you’re too much of a gentleman to allow your main method to “throw Exception.” Gasp!

The idea I am trying to get across is that Ruby is an exceptional tool for a programmer. Being an interpreted, purely object orientated, loosely typed programming language has huge benefits for writing short scripts like the one above. If you’re unfamiliar with Ruby you were probably shocked to see what the simple script did. You might be a little shocked to see the classic “Hello World” written Ruby style:

5.times { puts 'Hello World' }

Thats right, the Ruby version prints out “Hello World” five times because its so happy! No, there is no mistake above, I did just call a method on the number 5… get used to it. You’ve heard it all before, “Ruby is Purely Object Oriented.” Nearly everything is an object. Numbers and Strings especially! Here is a quickie, how characters are in this sentence?

'Here is a quickie, how characters are in this sentence?'.length

The above Ruby line of code spits out 55. What would you do? Please tell me you really didn’t count each letter… Maybe you pasted it in Microsoft Word, went to Tools, clicked Word Count and checked the number of characters. I don’t care how you do it, everyone has their favorite ways, my favorite is the fastest and most convenient way. Ruby is the answer. It has eclipsed my expectations when I started learning it and now I want it to blow your mind.

Heres something that might shock you. I’m going to put a method right into the String class and then I’m going to actually use it from a String:

class String
  def exclaim
    self + "!!!"
  end
end

puts "Ruby is fun".exclaim

The script, as expected, prints out:

Ruby is fun!!!

This is my first post about Ruby, and its meant to be harsh. I want you to be a little bit shocked, a little embarrassed, and most of all a wee bit interested. Because if you give Ruby a chance, you won’t regret it. I will post more about Ruby soon, so if you don’t already know Ruby then sit up straight and click on this link to learn Ruby in just 15 minutes:

Try Ruby - In Your Browser

If you try that link you will be presented with the interactive ruby prompt, the same prompt where I typed in my Ruby code and received instant feedback. The mastermind behind the above is also the author of the first tutorial listed below and a developer of Hackety Hack, a really neat Ruby starter kit. Its aimed at teens but I suggest that you download it because of these quick benefits:

  1. The same “Try Ruby Tutorial” is Built In
  2. A Great Ruby Cheat Sheet
  3. Extra Quick Tutorials
  4. File Organizer for your Ruby Scripts

After you get your feet wet from the above take a look at these Ruby tutorials which are themselves unlike any other tutorials you have ever read:

Why’s Poignant Guide to Ruby - The most talked about Ruby Tutorial ever written. No description I can put here can describe it. Just know that reading this might change your life… well maybe not your life but at least your perspective on what a technical tutorial can be.

Ruby’s Homepage - You were probably wondering when I might mention where you can actually download Ruby. Here is the home page for all things Ruby. Download it and start one of the tutorials from this website and in 30 minutes you will have a new favorite programming language.

Mr. Neighborly’s Humble Little Ruby Guide - Another great tutorial that gets deep quickly. Not as loose as “Why’s Guide” (but what else is?). Still this tutorial tops all the Java tutorials I have ever read.

There are so many links that I could throw at you but lets face it… if you take a chance with the 15 minute Ruby tutorial, then you will want to know more. These links give you everything you need to get started and days of joyful programing and hacking. Its your ambition that will take you further. Have a safe journey!


 
 
 

Leave a Reply


Recent Resources

Clicky Web Analytics