Little query

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?

Peter Hickman wrote:

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 :slight_smile:

Regards,

   Michael

Peter Hickman wrote:

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

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?

Hi,

Peter Hickman wrote:

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

···

On Wed, 21 Jul 2004 13:37:15 +0200, Florian Gross wrote:

+1

martin

···

Peter Hickman <peter@semantico.com> wrote:

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?

Hi,

···

In message "Re: Little query" on 04/07/21, Peter Hickman <peter@semantico.com> writes:

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.

Yukihiro Matsumoto wrote:

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!)

Yukihiro Matsumoto wrote:

Hi,

>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

···

In message "Re: Little query" > on 04/07/21, Peter Hickman <peter@semantico.com> writes:

"Michael Neumann" <mneumann@ntecs.de> schrieb im Newsbeitrag
news:40FE7141.6050809@ntecs.de...

Yukihiro Matsumoto wrote:
> Hi,
>
>
> >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

···

> In message "Re: Little query" > > on 04/07/21, Peter Hickman <peter@semantico.com> writes: