Editing Topic: RCR157 Project: RCR | 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?"


Back to RCRchive.


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