Ruby

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.
#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.eachdo  |i|
       puts #{i}
      end
    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:
HelloWorld
Hello Mathew
>>hello
>>namaste
>>gutan tag
UserName->mthomas, FullName->mathew thomas
mthomas


guru
ggg

To print 'Hello World' to the console
   puts "Hello World"

Create Methods
To define a new method
   
def echo(name)
       puts "Hello #{name}"
    end

Creates a new function, which we then call passing it the name value. I
f you want to provide default values for function parameters then
    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
   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
     @otherNames.each do  |i|
       puts #{i}
      end

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.

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • Trackbacks are closed for this entry.
Comments
  • No comments exist for this entry.
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.