ruby picture

RCR 157: = y

Submitted by flgr (Thu Oct 02 22:10:22 UTC 2003)


This is a legacy RCR from Ruby Garden, submitted by flgr. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

The following doesn't work:

def foo=(x, y)
  puts "called 'foo(#{x}.inspect) = #{y.inspect}'"
end = 10 # SyntaxError: compile error

Making this work would allow us to do interesting things like this one:

class Array
  def first=(*args)
    to, count = args.pop, args.pop || 1
    self[0, count] = to
  end  def last=(*args)
    to, count = args.pop, args.pop || 1
    self[(length - count) .. -1] = to
words = %w{hello world. how are you?} words.first(2) = %w{hi ruby!} words.last = %w{things going?} words.join(" ") # => "hi ruby! how are things going?"


(RCR imported from old format)



(RCR imported from old format)


(RCR imported from old format)
ruby picture
Comments Current voting

I'm neutral (Gavin, 2003-10-02 19:19:34)

I don't find the example given very compelling, but there are probably some decent uses for it. Overall, I'm pretty neutral, though.

No (, 2003-10-03 11:23:28)

For this to make sense would require making "def foo(x)=(y)" valid syntax. Not much gained either way in my opinion, but it's up to Matz to decide if he wants to parse that. I doubt it, though.

Re: No (flgr, 2003-10-03 20:20:04)

It wouldn't. After all it's also def []=(index, to) and not def [index]=(to).

Interesting (, 2003-10-06 08:30:33)

This seems counterintuitive to me, but maybe that is because I have some experience in functional languages.

I would expect syntax like that to function like this:

  words.first(2) = 3
  words.first(2)      # => 3
Allowing this syntax would be nice, because it would turn Ruby into a quasi-functional language(1), but I'm not sure Matz wants to go there. In any case, while I see what you're driving at, code like that would do what I'd expect it to do.

And, one question for you: does your syntax make more sense to you than replacing the "=" with a "!"?

words = %w{hello world. how are you?}
words.first!(2, %w{hi ruby!})
words.last!( %w{things going?} )
words.join(" ") # => "hi ruby! how are things going?"

(1) I don't know enough about functional languages to know what sort of worms would come out of this particular can, but I do like a lot of the features in Haskell, such as list comprehension, and provable function definitions.

Re: Interesting (flgr, 2003-10-09 02:02:54)

And, one question for you: does your syntax make more sense to you than replacing the "=" with a "!"?

IMHO bang versions of methods should do the same that non-bang versions do, but in-place instead of on a copy. -- I don't feel that words.first!(2, sth) fits into this model.

A Necessity!

I have a (sparse symmetric) matrix class which defines []=(i,j,a) for element assignment with bounds checking. Everything is working fine, and I decide it's time to use non-checked element assignment for internal use only.

I go helter-skelter with find-and-replace, changing all my self[i, j] = a to elem(i, j) = a. BOOM! 30 syntax errors.

I don't see a reason to give special status to []= over any other method name=().

Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .