Instance and class variable assignment

Discussion in 'Ruby' started by Daniel Schierbeck, Oct 18, 2005.

  1. I know this has been suggested before, but the only reason for rejection
    i found at the RCR archive was a dead end link...

    Can someone explain to me why this shouldn't be implemented in Ruby:

    def foo(@a, @b = "foo", @@c = "bar")
    # do something
    end

    # should do the same as this
    def foo(a, b = "foo", c = "bar")
    @a = a
    @b = b
    @@c = c
    # do something
    end

    I personally think it looks very interesting, especially since most
    initialize methods (at least most of those I write) just set instance
    and class variables.


    Cheers,
    Daniel
     
    Daniel Schierbeck, Oct 18, 2005
    #1
    1. Advertising

  2. Hrm, reminds me of an OptionParser trick that I like:

    options = Hash.new
    opts = OptionParser.new do |opts|
    opts.on('-r', '--reload', 'Reloads something') { |options[:reload]|
    }
    opts.on('-q' ,'--quiet', 'Squelches messages') { |options[:quiet]|
    }
    end

    That said, I think with method definitions I'd still avoid putting
    instance/class variables in the argument list.
     
    Kevin Ballard, Oct 18, 2005
    #2
    1. Advertising

  3. Selon Daniel Schierbeck <>:

    >
    > Can someone explain to me why this shouldn't be implemented in Ruby:
    >
    > def foo(@a, @b =3D "foo", @@c =3D "bar")
    > # do something
    > end
    >


    My eyes!!! >*_*<

    > # should do the same as this
    > def foo(a, b =3D "foo", c =3D "bar")
    > @a =3D a
    > @b =3D b
    > @@c =3D c
    > # do something
    > end
    >
    > I personally think it looks very interesting, especially since most
    > initialize methods (at least most of those I write) just set instance
    > and class variables.
    >


    I can see why you're interested (it shortens things quite a bit), but I t=
    hink
    it's a wrong idea. It's confusing to have method arguments that are not
    method-local. Isn't that why the current behaviour of block arguments is =
    going
    to be changed? :)
    --
    Christophe Grandsire.

    http://rainbow.conlang.free.fr

    It takes a straight mind to create a twisted conlang.
     
    Christophe Grandsire, Oct 18, 2005
    #3
  4. > Selon Daniel Schierbeck <>:
    > > Can someone explain to me why this shouldn't be implemented in Ruby:
    > >
    > > def foo(@a, @b =3D "foo", @@c =3D "bar")
    > > # do something
    > > end


    Yes. Matz doesn't like it. ;) (And I agree with Christophe -- my eyes! ;)

    -austin
    --
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Oct 18, 2005
    #4
  5. Austin Ziegler wrote:
    >>Selon Daniel Schierbeck <>:
    >>
    >>>Can someone explain to me why this shouldn't be implemented in Ruby:
    >>>
    >>> def foo(@a, @b = "foo", @@c = "bar")
    >>> # do something
    >>> end

    >
    >
    > Yes. Matz doesn't like it. ;) (And I agree with Christophe -- my eyes! ;)
    >
    > -austin
    > --
    > Austin Ziegler *
    > * Alternate:
    >
    >

    Bollocks :(
     
    Daniel Schierbeck, Oct 18, 2005
    #5
  6. On 10/18/05, Daniel Schierbeck <> wrote:
    > Austin Ziegler wrote:
    >>> Selon Daniel Schierbeck <>:
    >>>> Can someone explain to me why this shouldn't be implemented in Ruby:
    >>>> def foo(@a, @b =3D "foo", @@c =3D "bar")
    >>>> # do something
    >>>> end

    >> Yes. Matz doesn't like it. ;) (And I agree with Christophe -- my
    >> eyes! ;)

    > Bollocks :(


    It's a bit of an oversimplification to simply say that Matz doesn't
    like, but I do understand why. It isn't exactly intuitive to set
    instance variables in a method signature. (I'm not invoking POLS here,
    I'm suggesting instead that it would be unnecessarily confusing.) That
    said, it might be nice to have a language construct like C++'s
    initialization list:

    class Person
    {
    =09Person() : name(""), age(-1)
    =09{
    =09 // The rest of Person's default constructor here.
    =09}
    }

    -austin
    --
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Oct 18, 2005
    #6
  7. On Oct 18, 2005, at 10:03 AM, Christophe Grandsire wrote:

    > Selon Daniel Schierbeck <>:
    >
    >
    >>
    >> Can someone explain to me why this shouldn't be implemented in Ruby:
    >>
    >> def foo(@a, @b = "foo", @@c = "bar")
    >> # do something
    >> end
    >>
    >>

    >
    > My eyes!!! >*_*<
    >


    And what are the binding rules?

    def foo(@a = 1, @@b = @a)

    is equivalent to what? Common Lisp has two binding rules that
    generally used, e.g. let and let*. These would give you:

    def foo(a, b)
    old_a = @a
    @a = a
    @@b = old_a
    end

    and

    def foo(x, a, b)
    @a = a
    @@b = @a
    end

    You need both in CL.

    Cheers,
    Bob

    ----
    Bob Hutchison -- blogs at <http://www.recursive.ca/hutch/>
    Recursive Design Inc. -- <http://www.recursive.ca/>
    Raconteur -- <http://www.raconteur.info/>
     
    Bob Hutchison, Oct 18, 2005
    #7
    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. Sridhar R
    Replies:
    14
    Views:
    1,456
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Gerry Sutton
    Replies:
    1
    Views:
    584
    Peter Otten
    Apr 16, 2005
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    389
    Martin P. Hellwig
    Mar 26, 2010
  4. David Garamond
    Replies:
    5
    Views:
    279
    Ara.T.Howard
    Jun 8, 2004
  5. Leon Bogaert
    Replies:
    19
    Views:
    357
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page