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?"
def []=(index, to)
and not def [index]=(to)
.
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.
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.
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