Instance and class variable assignment

D

Daniel Schierbeck

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
 
K

Kevin Ballard

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.
 
C

Christophe Grandsire

Selon Daniel Schierbeck said:
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.
 
A

Austin Ziegler


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
 
B

Bob Hutchison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top