###In Ruby, is the attribute of an object an object?
To illustrate, let's define a Book class where each book (or each instance of Book) has an author.
class Book
attr_accessor :author
def author
@author
end
endWe can create a new instance of Book and call it new_book by saying:
new_book = Book.new
=> #<Book:0x007f974c94f238>We created a new instance of Book called new_book and it is stored as object #<Book:0x007f974c94f238>.
And to make sure that new_book is in fact an object, we can use the method #object_id, which returns an error if passed to anything other than an object.
new_book.object_id
=> 70143900809500What happens if we ask new_book who its author is?
new_book.author
=> nilWe get nil because we haven't yet told new_book who authored him.
Let's tell it:
new_book.author = "Rudyard Kipling"
=> "Rudyard Kipling"If we now ask new_book who its author is,
new_book.author
=> nilIt tells us that its author is "Rudyard Kipling".
Perfect! Now we can determine whether an attribute of an object is an object by passing #object_id to the author attribute of new_book. If it returns an object_id, we should conclude that attributes are objects.
new_book.author.object_id
=> 70143900735760So there you have it. In Ruby the attribute of an object is indeed an object.