[RCR] Array#push(empty array expanded) => no exception

This strange behavier really surprised me…

irb(main):006:0> [].push(*[])
ArgumentError: wrong number arguments (at least 1)
from (irb):6:in `push’
from (irb):6

Why is it illigal to concatenate an empty set ?
I expected Rubys Array class to work just like a normal set.
This seems VERY non-intuitive to me (POLS).

Array#unshift doesn’t like zero arguments neither.

An ugly hack to avoid it could be:
@data.unshift(*objs) unless objs.empty?

Can it be made legal to push/unshift with zero arguments ?

···


Simon Strandgaard

and what would it do? create a stack frame, and then return? to call a
function which does nothing is much more an expense than simply doing

.push * unless .empty?
.unshift * unless .empty?

or even

.push * rescue nil
.unshift * rescue nil

i think under normal conditions people would consider it an error to call a
method which becomes a no-op, without so much as a warning, when called with
no arguments. that would violate my POLS.

my 2 cents.

-a

···

On Tue, 17 Jun 2003, Simon Strandgaard wrote:

This strange behavier really surprised me…

irb(main):006:0> .push(*)
ArgumentError: wrong number arguments (at least 1)
from (irb):6:in `push’
from (irb):6

Why is it illigal to concatenate an empty set ?
I expected Rubys Array class to work just like a normal set.
This seems VERY non-intuitive to me (POLS).

Array#unshift doesn’t like zero arguments neither.

An ugly hack to avoid it could be:
@data.unshift(*objs) unless objs.empty?

Can it be made legal to push/unshift with zero arguments ?

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

Simon Strandgaard wrote:

This strange behavier really surprised me…

irb(main):006:0> .push(*)
ArgumentError: wrong number arguments (at least 1)
from (irb):6:in `push’
from (irb):6

Why is it illigal to concatenate an empty set ?
I expected Rubys Array class to work just like a normal set.
This seems VERY non-intuitive to me (POLS).

Array#unshift doesn’t like zero arguments neither.

An ugly hack to avoid it could be:
@data.unshift(*objs) unless objs.empty?

Can it be made legal to push/unshift with zero arguments ?


Simon Strandgaard

Already fixed in (later) versions of 1.8

ruby -v → ruby 1.8.0 (2003-06-14) [sparc-solaris2.9]

irb(main):004:0> .push(*)
=>

Regards,

Dan

I have no religion and im not religious, i am an agressive ateist.
I believe in Matz & Ruby, im starting to believe that matz is God!

does this sound scary?.. I better do cvs co 1.8 :slight_smile:

···

On Wed, 18 Jun 2003 04:20:16 +0900, Daniel Berger wrote:

Simon Strandgaard wrote:

Can it be made legal to push/unshift with zero arguments ?

Already fixed in (later) versions of 1.8


Simon Strandgaard

In general I think this kind of behavior is good. I like a method to
return the calling object if the method has no effect on the calling
object, but might have an effect if the argument had a different value.
For example, in the Set class,

set = Set.new([1,2])
set.delete(3)

returns set.

If you want to know whether the method did anything, you would do the
following

set.delete?(3)

which returns nil.

Regards,

Mark

···

On Tuesday, June 17, 2003, at 02:20 PM, Daniel Berger wrote:

[snip]

Already fixed in (later) versions of 1.8

ruby -v → ruby 1.8.0 (2003-06-14) [sparc-solaris2.9]

irb(main):004:0> .push(*)
=>

Regards,

Dan

Already fixed in (later) versions of 1.8

I just installed the Ruby-1.8.0-preview3 and it really rocks :wink:

ruby -v
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]
irb
irb(main):001:0> .push(*)
=>

BTW: the ‘rubicon’ seems not to test correct for this situation,
perhaps it should be fixed ??? see builtin/ArrayBase.rb:
def test_push
a = @cls[1, 2, 3]
assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))
Version.greater_or_equal(“1.6.2”) do
assert_exception(ArgumentError, “a.push()”) { a.push() }
end
Version.less_than(“1.6.2”) do
assert_equal(@cls[1, 2, 3, 4, 5], a.push())
end
assert_equal(@cls[1, 2, 3, 4, 5, nil], a.push(nil))
end

Apparantly this feature has been gone from Ruby 1.6.2 to 1.8.0-p3.

···

On Wed, 18 Jun 2003 04:20:16 +0900, Daniel Berger wrote:


Simon Strandgaard

Simon Strandgaard wrote:

Can it be made legal to push/unshift with zero arguments ?

Already fixed in (later) versions of 1.8

I have no religion and im not religious, i am an agressive ateist.
I believe in Matz & Ruby, im starting to believe that matz is God!

:slight_smile: IIRC, Matz believes in God, and he believes God != Matz

does this sound scary?.. I better do cvs co 1.8 :slight_smile:

On that we can all agree…

Hal

···

----- Original Message -----
From: “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, June 17, 2003 1:46 PM
Subject: Re: [RCR] Array#push(empty array expanded) => no exception

On Wed, 18 Jun 2003 04:20:16 +0900, Daniel Berger wrote:

From: “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, June 17, 2003 1:46 PM
Subject: Re: [RCR] Array#push(empty array expanded) => no exception

Simon Strandgaard wrote:

Can it be made legal to push/unshift with zero arguments ?

Already fixed in (later) versions of 1.8

guess i eat my words - not the first time…

I have no religion and im not religious, i am an agressive ateist.
I believe in Matz & Ruby, im starting to believe that matz is God!

:slight_smile: IIRC, Matz believes in God, and he believes God != Matz

hmmm. on this, he might be wrong.

-a

···

On Wed, 18 Jun 2003, Hal E. Fulton wrote:

----- Original Message -----

On Wed, 18 Jun 2003 04:20:16 +0900, Daniel Berger wrote:

====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

Simon Strandgaard wrote:

Can it be made legal to push/unshift with zero arguments ?

Already fixed in (later) versions of 1.8

I have no religion and im not religious, i am an agressive ateist.
I believe in Matz & Ruby, im starting to believe that matz is God!

:slight_smile: IIRC, Matz believes in God, and he believes God != Matz

Yeah, his implementation of Ruby shows it clearly:

tmp = God
=> :God
God = Matz
(irb):5: warning: already initialized constant God
=> :matz

It’s funny that Simon believes in him, and not in what he says :wink:

But actually this happens in quite some religions too.

···

On Wed, Jun 18, 2003 at 04:05:04AM +0900, Hal E. Fulton wrote:

On Wed, 18 Jun 2003 04:20:16 +0900, Daniel Berger wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  • JHM wonders what Joey did to earn “I’d just like to say, for the record,
    that Joey rules.”
    – Seen on #Debian

Simon Strandgaard wrote:

Can it be made legal to push/unshift with zero arguments ?

Already fixed in (later) versions of 1.8

guess i eat my words - not the first time…

If you think about it, it makes sense.

For one thing, what if a variable is passed in?
We don’t want it to fail just because the array
is empty.

For another thing, consider what happens with
strings. foo = “value” + “” should not be
illegal, I think.

Hal

···

----- Original Message -----
From: “ahoward” ahoward@fsl.noaa.gov
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, June 17, 2003 2:26 PM
Subject: Re: [RCR] Array#push(empty array expanded) => no exception

I believe in a paradise where opinions matters, where things isn’t static,
where you is able suggest improvents.

Cristianity is ancient and outdated… if I should use it, it should be
applyed some Ruby-patches first. BTW… matrix-reloaded is too religious
to be shown in Egypt ?

In Denmark you must pay cristianity-tax !!! If I just could change into
Ruby-religion, so I instead could pay Ruby-tax… I would feel less dirty.

···

On Wed, 18 Jun 2003 07:34:30 +0900, Mauricio Fernández wrote:

It’s funny that Simon believes in him, and not in what he says :wink:


Simon Strandgaard

If you think about it, it makes sense.

mostly.

For one thing, what if a variable is passed in? We don’t want it to fail
just because the array is empty.

well this is not exactly the case simon described though is it?

method # empty array as arg
method * # NO args!

so, it’s not that the method is failing just because an empty array is passed
in, it is failing because an empty array was exploded, yielding and
non-existent parameter list. i may be in the minority, but it’s not THAT
counter intuitive for me that attempting to call a method expecting args, with
no args, should be an error.

For another thing, consider what happens with
strings. foo = “value” + “” should not be
illegal, I think.

no, of course not. but this is analogous to the first example above, not the
second one, which would be more along the lines of

foo = “value” + *“”

if ‘*String’ did, for example, an explosion into a list of Fixnums (chars) or
something like that…

-a

···

On Wed, 18 Jun 2003, Hal E. Fulton wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

For one thing, what if a variable is passed in? We don’t want it to
fail
just because the array is empty.

well this is not exactly the case simon described though is it?

method # empty array as arg
method * # NO args!

Well, I meant more like this case:

myvar = mymethod # mymethod returns an array… possibly empty
meth2(*myvar) # Pass that array as individual args

so, it’s not that the method is failing just because an empty array is
passed
in, it is failing because an empty array was exploded, yielding and
non-existent parameter list. i may be in the minority, but it’s not THAT
counter intuitive for me that attempting to call a method expecting args,
with
no args, should be an error.

The asterisk (or star or splat) is a special case.
I can’t think of a case where Ruby generates a
“number of params” type of error for a method where
star is specified where the method is defined.

On the other hand, I am simply assuming that there
is a star on the definition. There certainly is one
on the call… they usually go together, UNLESS you
are expanding an array of known size, resulting in
exactly N parameters.

For another thing, consider what happens with
strings. foo = “value” + “” should not be
illegal, I think.

no, of course not. but this is analogous to the first example above, not
the
second one, which would be more along the lines of

foo = “value” + *“”

if ‘*String’ did, for example, an explosion into a list of Fixnums (chars)
or
something like that…

Well, I see what you mean. But they are analogous in
this sense: Given a collection of entities, add another
entity that is a “null” with respect to the context of
that collection and its add operation.

Pushing an empty list of items onto an array doesn’t
change the array, just as adding a null string onto
a string doesn’t change that string; except that we
are actually doing an assignment where the string
is concerned – better to make it a <<. Boy, this is
getting more and more nitpicky, isn’t it?

The bottom line, to me, is that if there is a star
in the definition AND in the caller, the idea of
“expanding” the array is almost meaningless.

Is there ever a real difference in these two situations?
(See below.) There probably is something I’m overlooking,
of course.

def meth1(*args)
#…
end

meth1(*myarray)

def meth2(args)
#…
end

meth2(myarray)

Cheers,
Hal

···

----- Original Message -----
From: “ahoward” ahoward@fsl.noaa.gov
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, June 17, 2003 4:07 PM
Subject: Re: [RCR] Array#push(empty array expanded) => no exception