Idea for Ruby 2.0

N

Nikolai Weibull

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first. The lowline works, but obviously for one
parameter, as it is a valid identifier. I was thinking whether Ruby 2.0
should introduce I-don't-care-type variables, like Haskell or Prolog
has. It would make all identifiers beginning with a lowline invalid,
except in parameter-lists, which could mean too much incompatability
with Ruby 1.x, but perhaps it's worth at least some thought?,
nikolai
 
B

Brian Schröder

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:
=20
[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }
=20
The idea is that I'm not interested in the second part of the internal
arrays, only the first. The lowline works, but obviously for one
parameter, as it is a valid identifier. I was thinking whether Ruby 2.0
should introduce I-don't-care-type variables, like Haskell or Prolog
has. It would make all identifiers beginning with a lowline invalid,
except in parameter-lists, which could mean too much incompatability
with Ruby 1.x, but perhaps it's worth at least some thought?,
nikolai
=20
--
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
=20
=20

Why would it make all identifiers beginning with an underscore
invalid? One don't care variable should be enough for everybody I
think ;-).

a, _, b, _, c =3D *function()

would make sense to me, though

a, _a, b, _b, c =3D *function()

could continue to set five variables.

Otherwise I like this at least on first sight. I indeed needed it for
some small scripts just a short while ago. There I used something like
my second example and simply ignored _a and _b, but the first example
would have been less intrusive to me.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Idea for Ruby 2.0"

|Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
|something like this:
|
|[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }
|
|The idea is that I'm not interested in the second part of the internal
|arrays, only the first.

How about

[[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

matz.
 
P

pavel.s.sokolov

Yukihiro said:
Hi,

In message "Re: Idea for Ruby 2.0"

|Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
|something like this:
|
|[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }
|
|The idea is that I'm not interested in the second part of the internal
|arrays, only the first.

How about

[[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

matz.

Hello,

What about
[[1, 2, 3], [2, 3, 4], [3, 4, 5]].each{ |first, *, last| fs << first,
last }
?
It does not work (and should not, as now). The idea was to skip some
part of incoming data.



BTW is there any way to skip a parameter when a method is called wich
has default parameters, like:

def aMethod( a = 1, b = 2)
#some stuff
end

aMethod( _, 4)# I do not want to change first parameter value and do
not know its default one.

BR,
Pavel
 
N

Nikolai Weibull

Brian said:
On 28/07/05, Nikolai Weibull
Why would it make all identifiers beginning with an underscore
invalid? One don't care variable should be enough for everybody I
think ;-).
=20
a, _, b, _, c =3D *function()
=20
would make sense to me, though
=20
a, _a, b, _b, c =3D *function()
=20
could continue to set five variables.

Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
nikolai

--=20
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
 
N

Nikolai Weibull

Yukihiro said:
Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first.
How about

[[1, 2], [2, 3], [3, 4]].each{ |first, *| fs << first }

that works _now_?

Hm, yeah. :). I had no idea, thanks. Still, it wouldn't currently
work in |a, *, c|,
nikolai
 
B

Brian Schröder

Brian Schr=F6der wrote:
=20
=20
Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
nikolai

But only for the sake of documentational value it works right now out
of the box. Just ignore variables beginning with an _ in your program,
and you are done. Or is your aim to optimize the time the assignment
takes?

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
N

Nikolai Weibull

Brian said:
But only for the sake of documentational value it works right now out
of the box. Just ignore variables beginning with an _ in your program,
and you are done. Or is your aim to optimize the time the assignment
takes?

Well, you can still reference them if you want to. I guess
optimization would be one goal. Perhaps I'm just clutching at straws,
nikolai

--=20
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
 
E

Eric Mahurin

--- Nikolai Weibull
Brian Schr=F6der wrote:
=20
=20
Well, you can still reference them if you want to. I guess
optimization would be one goal. Perhaps I'm just clutching
at straws,
nikolai

There is no reason Ruby couldn't optimize for the case where a
variable is declared in the argument list and never used.



=09
=09
__________________________________=20
Do you Yahoo!?=20
Yahoo! Mail - You care about security. So do we.=20
http://promotions.yahoo.com/new_mail
 
J

James Edward Gray II

Lately I've found myself using pseudo-anonymous variables a lot, e.g.,
something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of the internal
arrays, only the first.

Perl solves this by allowing you to assign to undef:

$ perl -e '($one, undef, $two) = split " ", "1 2 3"; print "$one $two
\n"'
1 3

Just FYI.

James Edward Gray II
 
N

Nikolai Weibull

Eric said:
There is no reason Ruby couldn't optimize for the case where a
variable is declared in the argument list and never used.

Nor for it to not issue a warning if the variable is declared in the
parameter list but never used,
nikolai
 
B

Brian Schröder

Underscore works as it is, so { |a, _, c| ... } works.
=20
T.


bschroed@black:~/svn/diplomarbeit/text$ irb
irb(main):001:0> def four_values() [1,2,3,4] end
=3D> nil
irb(main):002:0> a, _, b, _ =3D *four_values
=3D> [1, 2, 3, 4]

Ooops, everythings already there. I thought you could not assign in
parallel twice to the same variable. It seems it ain't really parallel
;)

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
D

Dave Burt

Trans said:
Underscore works as it is, so { |a, _, c| ... } works.

So does this: { |a, _, _, d| ... }

This doesn't: def foo(a, _, _, d)
.... but why not just do this: def foo(a, b, c, d)

And if you're after the case of a method in a subclass wanting to keep a
superclass' default parameter, one option is using a hash with named
arguments. But in this case I do see your point, and have felt the need
myself. The traits library handles stuff like this, I think.

Cheers,
Dave
 
N

Nikolai Weibull

Dave said:
This doesn't: def foo(a, _, _, d)
.... but why not just do this: def foo(a, b, c, d)

Yeah, I tried using it twice in a def but it didn't work, so I figured
it wouldn't work in a block parameter-list either, but it turns out that
it does. That was actually where I most wanted it.

Thank you all for your input,
nikolai
 
D

Daniel Brockman

Nikolai,
Lately I've found myself using pseudo-anonymous variables
a lot, e.g., something like this:

[[1, 2], [2, 3], [3, 4]].each{ |first, _| fs << first }

The idea is that I'm not interested in the second part of
the internal arrays, only the first. The lowline works,
but obviously for one parameter, as it is a
valid identifier.

Interestingly, blocks can have duplicate argument names,

{1=>2}.each {|_,_| puts _} #=> 2

but methods can't:

def foo _,_ ; end #=> SyntaxError
I was thinking whether Ruby 2.0 should introduce
I-don't-care-type variables, like Haskell or Prolog has.

As you said, it would be useful when *passing* arguments,
and it's obviously useful in blocks. I can see how it could
be worthwhile to allow it in method definitions too ---
for symmetry if nothing else.
It would make all identifiers beginning with a lowline
invalid, except in parameter-lists,

As others have pointed out, I'd think just one identifer
would need to be reserved: the single-underscore one.
which could mean too much incompatability with Ruby 1.x,
but perhaps it's worth at least some thought?

Even reserving just the single-underscore identifier would
mean some incompatibility, but then you *did* put "Ruby 2.0"
in the subject line. :)
 
D

Devin Mullins

Daniel said:
Interestingly, blocks can have duplicate argument names,
{1=>2}.each {|_,_| puts _} #=> 2
but methods can't:
def foo _,_ ; end #=> SyntaxError
That's because blocks don't actually have "arguments." They follow the
rules of parallel assignment.
http://phrogz.net/ProgrammingRuby/language.html#blocksclosuresandprocobjects
http://phrogz.net/ProgrammingRuby/language.html#parallelassignment

Devin
I decided that I can only post one email per day to this list, so as not
to be a total spammer. This is the one I chose. So sue me.

Maybe if I'm good, I'll up it to two.

Probably by next week, I'll have given up.
 
N

Nikolai Weibull

Daniel said:
As you said, it would be useful when *passing* arguments,
and it's obviously useful in blocks. I can see how it could
be worthwhile to allow it in method definitions too ---
for symmetry if nothing else.
^^^^^^^^^^^^

Precisely so.
Even reserving just the single-underscore identifier would
mean some incompatibility, but then you *did* put "Ruby 2.0"
in the subject line. :)

Yeah, still, perhaps it can be flexible enough to allow it as a valid
identifier if it is actually being used in the body of whatever's using
it...,
nikolai
 
N

Nikolai Weibull

This should not happen anyway. There are enough good reasons to
ignore some parameters (cf. Integer#times).

That's precisely why you would want to have unnamed variables,
nikolai
 
D

David Vallner

Nikolai said:
Well, it depends on how you would want it. In Prolog (don't remember
how Haskell does it), all variable beginning with a lowline are ignored
- it adds some documentational value. Perhaps only _ is enough,
nikolai
Offtopic: I thought those are just any old variables in Prolog. I know
for 25% certain SWI-Prolog uses variables starting as underscores as
anonymous variables it creates when getting itself into an infinite loop
fit ;P

David
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top