Forum: Ruby on Rails def [](tag) ....find_by_name(tag.to_s).......end

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.
Raj S. (Guest)
on 2007-01-01 19:09
(Received via mailing list)
Given below is a model from mephisto blogging tool.

class Tag < ActiveRecord::Base
  @@tag_parse_regex = /((?:
|)['"]{0,1})['"]?\s*(.*?)\s*(?:[,'"]|$)(?:\1(?:
|$))/
  has_many :taggings

  class << self
    def [](tag)
      find_by_name(tag.to_s)
    end

.................
..................


How to consume the above method and why it in this confusing way. Why
the
name of the method is [] ...seems like an index to me. Am I supposed to
consume this method Tag.[1] ?

Thanks.
Duane J. (Guest)
on 2007-01-02 01:58
(Received via mailing list)
On Jan 1, 2007, at 10:07 AM, Neeraj K. wrote:

>     end
>
Yes, this is how you define the square-bracket method in ruby.  You
do not need a "." to call it, like other methods:

Tag[:something]

Duane J.
(canadaduane)
http://blog.inquirylabs.com/
Raj S. (Guest)
on 2007-01-02 05:00
(Received via mailing list)
Thanks.
This topic is locked and can not be replied to.