Thursday, January 20, 2011

Ruby interviews questions and answer

How can you say that ruby is very object oriented language?

Ruby is very object oriented. Everything is an object -- even things you might consider constants. This also means that the vast majority of what you might consider "standard functions" aren't floating around in some library somewhere, but are instead methods of a given variable.
Here's one example we've already seen:
3.times { puts "Hi!" }
Even though 3 might seem like just a constant number, it's infact an instance of the class Fixnum (which inherits from the class Numeric which inherits from the class Object). The method times comes from Fixnum and does just what it claims to do.
Here are some other examples
$ irb --simple-prompt
>> 3.abs
=> 3
>> -3.abs
=> 3
>> "giraffe".length
=> 7
>> a = "giraffe"
=> "giraffe"
>> a.reverse
=> "effarig"

No comments:

Post a Comment