I think you are just confused by the overloaded use of the words objct
and class as opposed to the ruby classes named Object and Class.


This diagram / project might help clarify

http://objectgraph.rubyforge.org/dotOG.html

From

http://objectgraph.rubyforge.org/

Max

On 5/16/11, John Merlino <li...@ruby-forum.com> wrote:
> Hey all,
>
> I am a little confused about a tutorial I read. Here's an example below
> taken from the tutorial:
>
> Object.instance_eval do
>    def has_attribute( *attrs )
>        attrs.each do | attr |
>            self.class_eval %Q{
>                 def #{attr}=(val)
>                     instance_variable_set("@#{attr}", val)
>                 end
>
>                 def #{attr}
>                     instance_variable_get("@#{attr}")
>                 end
>            }
>        end
>    end
> end
>
> class A
>    has_attribute :my_attribute, :another_attribute
> end
>
> a = A.new
>
> puts a.methods - Object.methods
> # => ["my_attribute", my_attribute=", "another_attribute",
> "another_attribute="]
>
> a.my_attribute = 1
> a.my_attribute
> # => 1
>
> a.another_attribute = "A String"
> a.another_attribute
> # => "A String"
>
> THe guy says:
> "The first instance_eval is used to add class method has_attribute into
> Object so that we can call it in all the inherited class."
>
> I'm a little thrown off by this statement. First, Object itself is an
> instance of class Class. Someone here even said before "Object is an
> instance of Class". So "class Object end "implies that Object is an
> instance of class, equivalent to: Object = Class.new. Hence, we can
> invoke instance_eval on Object, since Object is an instance. What
> confuses me here is has_attribute appears to be an instance method (e.g.
> def a end) not class method (e.g. def self.a end). So why does guy say
> we add class method has_attribute?
>
> In fact, the difference between Object class and Class class is that
> when you add methods to class Class, those methods become available to
> any class that is created as well (since all classes created using class
> construct inherit from Class) and you can use those methods within any
> class. However, that's not what we do above. We extend Object above.
> When you instantiate a class, you inherently create an object instance,
> so that object instance should have available the methods defined in
> Object. So then how can we access that method within a class, such as
> that being done in class A above?
>
> Thanks for response
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to