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
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