"<" method for array

Hi,

I hate there is not a "<" method for array object.
Since there is the "<<" for adding an element, why not have the "<" for truncating then filling up the array?

I know I can write one:

irb(main):009:0> class Array
irb(main):010:1> def <(a)
irb(main):011:2> clear
irb(main):012:2> self << a
irb(main):013:2> end
irb(main):014:1> end

But it's not that convenient when I want it.

Thanks!

That would surprise the hell out of me.

Established convention is that << is the "left shift" or "append"
operator, which will change the receiver. On the other hand, < is the
"less than" operator, which simply compares the receiver to the
argument, no change whatsoever.

You *can* redefine < to do whatever you want, but I'm not sure why you
would define it this way.

···

On Nov 25, 10:49 pm, "Eva" <eva54...@sina.com> wrote:

Hi,

I hate there is not a "<" method for array object.
Since there is the "<<" for adding an element, why not have the "<" for truncating then filling up the array?

I know I can write one:

irb(main):009:0> class Array
irb(main):010:1> def <(a)
irb(main):011:2> clear
irb(main):012:2> self << a
irb(main):013:2> end
irb(main):014:1> end

But it's not that convenient when I want it.

--
-yossef

what do you mean? (apology for my english is poor)

kind regards -botp

···

2010/11/26 Eva <eva54321@sina.com>:

But it's not that convenient when I want it.

Eva wrote in post #964024:

I hate there is not a "<" method for array object.

If there were, I would expect it to work like this:

class Array
include Comparable
end
[1] < [2]

=> true

[1] < [1]

=> false

[1] < [1,0]

=> true

Since there is the "<<" for adding an element, why not have the "<" for
truncating then filling up the array?

Array#replace already does that, except you have to give it the element
also wrapped in an Array.

a = [1,2,3]

=> [1, 2, 3]

a.object_id

=> 69928599426960

a.replace [4]

=> [4]

a

=> [4]

a.object_id

=> 69928599426960

···

--
Posted via http://www.ruby-forum.com/\.