Most IT managers know about the precious stone ruby and not the programming language Ruby. Its a sad state of affairs where career IT managers are so far disconnected from what is happening in the technology world. Not much you and I can do about that…so let me do some Ruby”ing” here.
The point I want to emphasize in this blog is that Ruby reduces a lot of noise in languages such as Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#just print out hello world puts "Hello World" # create a method to print hello world def echo(name) puts "Hello #{name}" end # invoke it echo("Mathew") # define a user info class class User attr_accessor :userName attr_accessor :fullName attr_accessor : otherNames def initialize(userName = "none", fullName = "",otherNames="") @userName = userName @fullName = fullName @otherNames = otherNames greetings = ["hello", "namaste", "gutantag"] for i in 0...greetings.length puts">>" + greetings[i] end end def printOtherNames if @otherNames.nil? puts "[no othernames]" elsif @otherNames.respond_to?("each") @otherNames.each {|i| puts "#{i}"} end end def toString puts "UserName->" + @userName +", FullName->" + @fullName end end # create an instance and call echo e = User.new("mthomas", "mathew thomas", ["guru", "ggg"]) e.toString() # print the attr value puts e.userName puts e.printOtherNames |
Executing the above using the Ruby interpreter gets us:
1 2 3 4 5 6 7 8 9 10 11 12 |
Hello World Hello Mathew >>hello >>namaste >>gutantag UserName->mthomas, FullName->mathew thomas mthomas guru ggg guru ggg |
To print ‘Hello World’ to the console
1 2 |
puts "Hello World" |
Create Methods
To define a new method
1 2 3 4 |
def echo(name) puts "Hello #{name}" end |
Creates a new function, which we then call passing it the name value. If you want to provide default values for function parameters then
1 2 |
def echo(name="none") |
Creating Classes
We create a class User to hold some basic information. The method initialize is the constructor. You refer to class variables as @fullname. Keyword attr_accessor allows us to access the value of the member variable like e.userName. Without this keyword we would not be able to access the member variable.
Arrays/Lists
1 2 |
greetings = ["hello", "namaste", "gutan tag"] |
Declares a variable named ‘greetings’ with a list of strings. You do not need to specify types for your variables. Ruby figures out that based on the value you assign it. You can even change the type later by assigning a different value to varaible.
Closures
Support for closures, which are blocks of code that you can define and pass around. Look at the code sample below
1 2 |
@otherNames.each {|i| puts "#{i}"} |
The class variable @otherNames is a list of strings. Note for the java programmer. Everything in Ruby is an object. So your variable is an object that inherits various methods from base classes in Ruby. All this happens auto-magically for you in Ruby. If the variable is an array then you have this method named ‘each’ which can be used to go over each and every item in the array. In the above example we go over each element and apply a block of code to execute against that element. The |i| is a temporary variable that holds the current element.
In Conclusion
What attracts me to Ruby is that it removes a lot of the noise out of my code. I am not giving up on Java anytime soon, but its definitely worth every techie’s time to do some Ruby or any other dynamic language (try jython…though I hate the indentation approach). There is also JRuby which is a pure java implementation of the Ruby language.
Having more languages ported to run on the VM is a good thing….and of course Sun has to do that since Microsoft is doing this for their languages on top of CLI. For the Java platform we already have Jython, JRuby and then there is Groovy.
But in the end of the day, if I were starting a brand new web project why not just develop using Ruby and Ruby On Rails. Sure we could use Groovy and Grails but for many applications tight integration with current J2EE libraries is not needed. In future we can use Jython 2.5 and Django too. I think the bigger challenge is finding enough good people to write in these new languages. Once that is achieved….we need another set to maintain them. Ruby code being so compact can sometimes be difficult to read. But then massive code bases in Java can be just as hard.