Ruby tutorial session 12

Everything is an Object:

  • 2 + 2 is equivalent to 2+(2) and 2.send(:+, 2)
  • 2.hours.ago
  • 2.class # => Fixnum
  • 2.class.instance_methods – Object.instance_methods
  • “andreas”.capitalize

Constants:

  • Constants defined in a class/module are available within that class/module and outside the class with the scope operator ::
  • Constants defined outside any class/module can be accessed anywhere
  • Constants cannot be defined in methods

Introspection:

andreas = Person.new("Andreas") 
andreas.inspect # => #<Person:0x1cf34 @name="Andreas"> 
 
andreas.class # => Person 
andreas.class.superclass # => Object 
andreas.class.superclass.superclass # => nil 
 
andreas.class.ancestors.join(", ") # Person, Object, Kernel 
 
Person.instance_methods(false) # => say_hi 
Kernel.methods.sort.join("\n") # => All methods in Kernel module  

Hope you learn something to day.

Follow this blog By subscribing.

Thanks


No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...