Can someone explain me this Ruby on Rails puzzle?
class Post < ActiveRecord::Base
has_many :comments
end
Post.first.comments.class
=> Array
Array === Post.first.comments
=> false
Array === [ 1 ]
=> true
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Post.first.commentsis a delegator. It does not give you back an Array directly, but if you do anything with it, it turns into one. This is useful because it lets you do stuff likewithout having to inject those methods into the array object, or extending the
Arrayclass.