Little query

P

Peter Hickman

Given that x is an Array how come I can do

if x.last + 1 == value

but I cant do

x.last = value

I have to use

x[-1] = value

This switching notation is rather off-putting as I am trying to use the
more readable .first .last notation

Any reason why x.last = value doesn't work?
 
M

Michael Neumann

Peter said:
Given that x is an Array how come I can do

if x.last + 1 == value

but I cant do

x.last = value

you could do, if there was a Array#last= method. Try:

class Array
def last=(v)
self[-1] = v
end
end

and now:

x.last = value

works :)

Regards,

Michael
 
P

Peter Hickman

Thanks that was very useful.

Is there any chance that

class Array
def first=(v)
self[0] = v
end

def last=(v)
self[-1] = v
end
end

might become part of the core? matz?
 
F

Florian Gross

Peter said:
Any reason why x.last = value doesn't work?

Because then x.last(5) = [1, 2, 3, 4, 5] would also have to work, but
this is currently syntactically impossible.

I once submitted a RCR to allow that syntax, but it wasn't clear whether
the community or matz really wanted to have this:

http://www.rcrchive.net/rcr/RCR/RCR157

Regards,
Florian Gross
 
M

Martin DeMello

Peter Hickman said:
Is there any chance that

class Array
def first=(v)
self[0] = v
end

def last=(v)
self[-1] = v
end
end

might become part of the core? matz?

+1

martin
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Little query"

|Is there any chance that
|
|class Array
| def first=(v)
| self[0] = v
| end
|
| def last=(v)
| self[-1] = v
| end
|end
|
|might become part of the core? matz?

I'm not enthusiastic for them. I feel like that they are not as
useful as, "push" method for example.

matz.
 
P

Peter Hickman

Yukihiro said:
I'm not enthusiastic for them. I feel like that they are not as
useful as, "push" method for example.

matz.
For me it seems natural that if I can access the value from x.first I
would also be able to update x.first.

It's not a great deal but first and last read better that [0] and [-1]
and I would like to use them all the time without forever having to
overload the array class.

I can live with it but having been away from Ruby for a bit this is the
first thing I tripped up on (other than the use of ; to end lines!)
 
M

Michael Neumann

Yukihiro said:
Hi,

In message "Re: Little query"

|Is there any chance that
|
|class Array
| def first=(v)
| self[0] = v
| end
|
| def last=(v)
| self[-1] = v
| end
|end
|
|might become part of the core? matz?

I'm not enthusiastic for them. I feel like that they are not as
useful as, "push" method for example.

Me too. Then, there should be also Range#last= and Range#first=, but I
don't like them too much.

Regards,

Michael
 
R

Robert Klemme

Michael Neumann said:
Yukihiro said:
Hi,

In message "Re: Little query"

|Is there any chance that
|
|class Array
| def first=(v)
| self[0] = v
| end
|
| def last=(v)
| self[-1] = v
| end
|end
|
|might become part of the core? matz?

I'm not enthusiastic for them. I feel like that they are not as
useful as, "push" method for example.

Me too. Then, there should be also Range#last= and Range#first=, but I
don't like them too much.

IMHO Range is a different cup of tea because Ranges are immutable. So
assigning would not be approrpiate here.

robert
 
K

Kristof Bastiaensen

On Wed, 21 Jul 2004 13:37:15 +0200, Florian Gross wrote:
Hi,
Peter said:
Any reason why x.last = value doesn't work?

Because then x.last(5) = [1, 2, 3, 4, 5] would also have to work, but this
is currently syntactically impossible.

I once submitted a RCR to allow that syntax, but it wasn't clear whether
the community or matz really wanted to have this:

I'd like that. I would use this to acces class variables as
an collection. For example: myobj.names(2) = "myname"
It would work like myobj.names[2] = "myname", but call the method
on myobj in stead of myobj.names.

Regards,
Kristof
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top