Dumb question to which I ought to know the answer by now

Mark J. Reed wrote:

Once I’d proposed similar;

hash[*keys] = *values

That would be handy, but I don’t think it’s possible to define such
a method in current Ruby. The parameters to = are flattened;
you wouldn’t be able to tell where the keys stopped and the values
started, unless you assumed that keys.length == values.length,
which
is not always a valid assumption where I’ve seen the Perl idiom
used.

I think in Nobu’s code, keys and values are separate arrays, so there
is no need to know where keys stop and values begin. Or have I
misread?

···

On Fri, Dec 05, 2003 at 11:32:51PM +0900, nobu.nokada@softhome.net wrote:


Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

Hi –

Mark J. Reed wrote:

Once I’d proposed similar;

hash[*keys] = *values

That would be handy, but I don’t think it’s possible to define such
a method in current Ruby. The parameters to = are flattened;
you wouldn’t be able to tell where the keys stopped and the values
started, unless you assumed that keys.length == values.length,
which
is not always a valid assumption where I’ve seen the Perl idiom
used.

I think in Nobu’s code, keys and values are separate arrays, so there
is no need to know where keys stop and values begin. Or have I
misread?

I think what Mark means is that if you’ve got this:

def =(keys,values)

and you do what amounts to this:

hash[1,2,3] = 4,5,6

then in effect you’ve got a call like this:

hash=(1,2,3,4,5,6)

which doesn’t fit the two-arg format, and has no unambiguous dividing
line (unless you decide that the key and value lists have to be of the
same length, which I actually think would be reasonable, though as
Mark says not identical to the Perl idiom [which I don’t mind :slight_smile: ]).

David

···

On Sat, 6 Dec 2003, Michael Campbell wrote:

On Fri, Dec 05, 2003 at 11:32:51PM +0900, nobu.nokada@softhome.net > wrote:


David A. Black
dblack@wobblini.net

I certainly don’t mind diverging from Perl; there’s a reason I
swtiched from Perl to Ruby for my daily scripting tasks, after
all. :slight_smile: But it is occasionaly convenient to be able to use a longer
key array (which sets the unmatched keys to undef/nil) or a longer
value array (in which case unmatched values are ignored).

-Mark

···

On Sat, Dec 06, 2003 at 01:10:42AM +0900, David A. Black wrote:

hash=(1,2,3,4,5,6)

doesn’t fit the two-arg format, and has no unambiguous dividing
line (unless you decide that the key and value lists have to be of the
same length, which I actually think would be reasonable, though as
Mark says not identical to the Perl idiom [which I don’t mind :slight_smile: ]).

That is indeed what I meant, but it turns out not to be the case.
If you do this:

hash[1,2,3] = 4,5,6

it turns out to be equivalent to this:

hash.[]=(1,2,3,[4,5,6])

so the key/value division is done for you by Ruby (all the values are in the
last argument), which is how the hashslice module in the RAA works.
So I decided to use that module.

Thanks to everyone for their replies.

-Mark

···

On Sat, Dec 06, 2003 at 01:10:42AM +0900, David A. Black wrote:

I think what Mark means is that if you’ve got this:

def =(keys,values)

and you do what amounts to this:

hash[1,2,3] = 4,5,6

then in effect you’ve got a call like this:

hash=(1,2,3,4,5,6)