Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialUlfar Ellenarson
5,277 Pointsthe method def find(name)
How does the block "instances.find do |instance| instance.name == name end " work? My specific question is where is the method name defined that allows instance.name?
def find(name) instances.find do |instance| instance.name == name end end
I understand the find method on instances as instances is an array and the find method is from enumerables and the enumerables mixin is contained in arrays. I don't understand is where the the name method comes from. But I am guessing that it comes from the attr_accessor :name, however I thought attr_accessor were classes.
Would someone be so kind and expound on how instance gets the method name.
Francois van der Hoven
2,026 PointsFrancois van der Hoven
2,026 PointsHi Ulfar, when I see the term instance.name in a ruby class, it tells me that instance is a the name of a variable that represents an object. An object may have properties defined in the class definition. Therefore, instance.name refers to the 'name' property of the instance class.
A class sometimes have instance variables. If an instance variable is meant to be changed it appears in the list following attr_accessor. In other words, after the declaration attr_accessor folows a list of all the properties that may be modified, for example instance.name = 'new name'. Here is a link that gives you a more in-depth explanation of attr_accessor: http://www.rubyist.net/~slagell/ruby/accessors.html.