Ruby classes reloaded

1334561763000 » Tagged as: Ruby

Whilst learning Ruby with the help of the Humble Little Ruby Book, I needed to do a little bit of extra reading to get a clear picture on classes and objects. This a study note made from that. write, << and puts all expect objects that are strings or can be converted to strings (object that have the .to_s method). That means you cant write code that looks like  “bada” + 123 , it will produce an error. Instead you have to use puts “bada” + 123.to_s . If you want to be able to pass your own classes to puts or other methods that deal exclusively with strings, You can  implement the to_s method. It is the equivalent of java’s toString();  

We have already discussed that you can override methods in classes in an adhoc manner, but there is no way to overload methods. A great pity.

self was previously put forth as a reference to the current object, however it can also be used to define 'static' methods, better known as class scope methods.

  def self.class_scope_method

    some code


Ruby reflection if you can call it that, is rather interestingly. You can call instance_methods on an object and it will produce a listing of all the methods that are available.

Some of the tidbits in this post  is thanks largely to knowledge gathered from and some of it from

comments powered by Disqus