Forum: Ruby where does song.name come from?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2006-12-28 04:56
(Received via mailing list)
Hi all,

On page 49 of Pickaxe 2 are some scripts:

class Songlist
  def with_title(title)
     @songs.find{|song| title==song.name}
  end
end

I just wonder where the method "name" comes from and
what its purpose is here. I check for Index on page
809 and find "name" is mentioned in class Module only,
which returns the name of module mod.

Thanks,

Li
F3b02532d4cb4855881935c002389213?d=identicon&s=25 Morton Goldberg (Guest)
on 2006-12-28 06:27
(Received via mailing list)
On Dec 27, 2006, at 10:55 PM, chen li wrote:

> I just wonder where the method "name" comes from and
> what its purpose is here. I check for Index on page
> 809 and find "name" is mentioned in class Module only,
> which returns the name of module mod.

Very much earlier in the book (p. 26 in the PDF version) the class
Song was defined as follows:

class Song
    def name
       @name
    end
    def artist
       @artist
    end
    def duration
       @duration
    end
end

That's where 'name' comes from. It's a reader method and it's purpose
is to return the value of  the instance variable Song::@name. It has
nothing to do with the method Module::name.

Regards, Morton
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2006-12-28 06:41
(Received via mailing list)
chen li wrote:
> On page 49 of Pickaxe 2 are some scripts:
>
> class Songlist
>   def with_title(title)
>      @songs.find{|song| title==song.name}
>   end
> end
>
> I just wonder where the method "name" comes from and
> what its purpose is here.

Earlier in the book there is:

class Song
  def name
    @name
  end
  ...
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2006-12-28 15:01
Gavin Kistner wrote:
> >
> Earlier in the book there is:
>
> class Song
>   def name
>     @name
>   end
>   ...


Thanks.

The methods about class Song are everywhere on different pages.
Sometimes I get lost. I think it would be nice if somewhere in the
book(such as appendix) class Song and all its methods are put together.


Li
This topic is locked and can not be replied to.