Comment on this RCR (edit wiki page) | RCRchive home

RCR 157: obj.foo(x) = y

submitted by flgr on 2003-10-02 18:24:22


This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.


The following doesn't work:

def foo=(x, y)
  puts "called 'foo(#{x}.inspect) = #{y.inspect}'"
end

self.foo(5) = 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
  end
end

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?"

Vote for this RCR

Strongly opposed [3]
Opposed [3]
Neutral [1]
In favor [3]
Strongly advocate [3]

Change the status of this RCR to:

accepted

rejected

withdrawn


Add comments here

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=().


Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog