Ruby Basics Page - II : Objects and Messages / Methods in Ruby
/*********************************************** * Image Thumbnail Viewer Script- © Dynamic Drive (www.dynamicdrive.com) * This notice must stay intact for legal use. * Visit http://www.dynamicdrive.com/ for full source code ***********************************************/
Ruby like many other programming languages is Object Oriented. So lets create an Object and define a bare-bones message /method(for people like me coming from Java world) on it. People coming from Java might find the syntax a little weird perhaps (I did at first), but thats the nature of Ruby in itself (No, i am not insinuating that Ruby is weird
).
# Object " obj " created here (using new)
obj = Object.new
#object_id is an "in-built" method in Ruby for generic our
#generic object.
puts("An object created ! with Id --->",obj.object_id)
# Now the weird part, lets define a method
#(I will call it capability or a message
#(more accurate term is message) for obj).
# For a java programmer this might be weird
#because you are defining methods on "fly"
# (as your object has already been created).
def obj.talk
puts("I can talk now! wee hee")
puts("Can you?")
# Chomp trims out a new line char
answer = gets.chomp
if(answer == ("Yes")) then
puts("Ok, lets Talk!")
else
puts("Silence is Golden!")
end
end
# Call Talk here!
obj.talk
lets break down as to what is going on here with a bit of details:
- First we create a new object, with Object.new
- Then we print the object with an id, using curious looking object_id, this is an inbuilt method err message in Ruby
. - Next we define object’s method! This part might be weird to those who come from Java or even C++ world. In those languages you define methods within a class. Once you initialize an Object (instance of that class) you can be sure that Object will have finite or limited set of behavior and attributes defined at the time of class definition. Here we are defining methods after the fact that a generic object with no capabilities has been initialized. This concept in itself is worth “gulping” because you can start with an object with limited or no capabilities and add things as you move along. This is an example of how dynamic Ruby really is.
- Nothing special about method definition really, we use something called a “def” keyword followed by the object variable on which this method/ message is being defined for, followed by a DOT operator and the name of the message / method. In our case it is “talk”
- gets uses a chomp to trim off the trailing newline character that gets will automatically add, this is helpful in comparisons apparently
- Next we show IF-ELSE construct in ruby, the difference from Java or C++ is that it is a bit verbose, uses a then and also an end to well … end the block
- finally we call the talk method or pass talk message to the obj to see if it understands it, if it doesn’t there will be error handling done (more on that later).
Well thats pretty much it, lets see the output of this cute little program:
Click here to see the output!
Thats it for now, More to follow on this later.
Thanks for your time guys! keep reading!
Regards
Vyas, Anirudh