I'm reading through part 1, blocks and iterators section. Lots to
digest here
Anyway, there is discussion about implementing a search for songs,
called 'with_title'
Even though the first code example is not the one that ultimately gets
used I think for me it's important to understand it, so I tried
recreating it outside the actual application. I'm getting some errors
and would appreciate some feedback.
Here is the code (I've used some puts statements to see what's going on)
def with_title (title)
myarray = ['Ruby Tuesday', 'Paint it Black', 'Lets Spend the Night Together',
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽'Mothers Little Helper', 'Jumpin Jack Flash']
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽for i in 0...myarray.length
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽puts myarray.length # added in to debug
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽puts 'here is i: ' + i.to_s # added in to debug
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽return myarray[i] if title == myarray[i].title
#leaving .title in causes an error
聽聽聽聽聽#about undefined method
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽puts 'here is myarray[i] ' + myarray[i]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽end
聽聽聽聽聽聽聽聽聽聽聽聽end
聽聽聽聽聽聽聽聽聽聽聽聽with_title('Paint it Black')
If in this line
return myarray[i] if title == myarray[i].title <------ I removed the
.title from the right side of
the equality statement, myarray[i]
I get this back:
5
here is i: 0
here is myarray[i] Ruby Tuesday
5
here is i: 1