syntax quickie

Discussion in 'Ruby' started by Kev Jackson, Oct 24, 2005.

  1. Kev Jackson

    Kev Jackson Guest

    Hi all,

    What exactly does the following do and how can I test it? It looks like
    its creating a new value with the key name, but how do you call it?

    def [](name)
    @val[name]
    end

    Any help appreciated

    Kev
     
    Kev Jackson, Oct 24, 2005
    #1
    1. Advertising

  2. Selon Kev Jackson <-vn.com>:

    > Hi all,
    >
    > What exactly does the following do and how can I test it? It looks lik=

    e
    > its creating a new value with the key name, but how do you call it?
    >
    > def [](name)
    > @val[name]
    > end
    >
    > Any help appreciated
    >


    Just call it using array notation: foo[name]. Array notation is just synt=
    actic
    sugar for the method [](argument), i.e. foo[name] is the same as foo.[](n=
    ame).

    So basically you have here an object that contains a hash as an instance
    variable (named @val, not the clearest name ever chosen ;) ), and allows =
    you to
    directly read the values in that hash using array notation, as if the obj=
    ect was
    a hash itself.
    --
    Christophe Grandsire.

    http://rainbow.conlang.free.fr

    It takes a straight mind to create a twisted conlang.
     
    Christophe Grandsire, Oct 24, 2005
    #2
    1. Advertising

  3. Kev Jackson

    Kev Jackson Guest

    Christophe Grandsire wrote:

    >Selon Kev Jackson <-vn.com>:
    >
    >
    >
    >>Hi all,
    >>
    >>What exactly does the following do and how can I test it? It looks like
    >>its creating a new value with the key name, but how do you call it?
    >>
    >>def [](name)
    >> @val[name]
    >>end
    >>
    >>Any help appreciated
    >>
    >>
    >>

    >
    >Just call it using array notation: foo[name]. Array notation is just syntactic
    >sugar for the method [](argument), i.e. foo[name] is the same as foo.[](name).
    >
    >So basically you have here an object that contains a hash as an instance
    >variable (named @val, not the clearest name ever chosen ;) ), and allows you to
    >directly read the values in that hash using array notation, as if the object was
    >a hash itself.
    >--
    >Christophe Grandsire.
    >
    >

    Thanks, so I had actually accessed it by accident anyway :).

    Kev
     
    Kev Jackson, Oct 24, 2005
    #3
  4. Kev Jackson

    Dido Sevilla Guest

    On 10/24/05, Kev Jackson <-vn.com> wrote:
    > Hi all,
    >
    > What exactly does the following do and how can I test it? It looks like
    > its creating a new value with the key name, but how do you call it?
    >
    > def [](name)
    > @val[name]
    > end
    >


    It's actually Ruby's syntax for operator overloading. An instance of
    a class that defines this can be "indexed" as though it were an array
    or hash. Other operators are overriden in the same way, e.g.

    def +(x)
    ...
    end

    Remember that everything in Ruby is an object, so when you see even
    something as simple as 1 + 1 in Ruby, that's actually syntactic sugar
    for 1.+(1), calling the + method for an instance of the Fixnum class.
     
    Dido Sevilla, Oct 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G

    Javascript Quickie

    Craig G, Jan 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    419
    Craig G
    Jan 25, 2005
  2. Roy

    Quickie: Printing out code?

    Roy, Oct 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    418
  3. Roy
    Replies:
    1
    Views:
    593
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 26, 2006
  4. Gene Ellis
    Replies:
    0
    Views:
    514
    Gene Ellis
    Aug 25, 2004
  5. bartek
    Replies:
    3
    Views:
    688
    Victor Bazarov
    Mar 7, 2004
Loading...

Share This Page