From: "David A. Black" Date: 2004-11-15T06:07:10+09:00 Subject: Re: I guess "obj.attr = arg" methods don't allow blocks... (maybe rcr?) Hi -- On Mon, 15 Nov 2004, Sam Roberts wrote: > > Does this conflict with some kind of basic rule in the grammer? Well... I suspect it conflicts with a basic intention, which is to make these = methods callable in a way that looks and acts as close to assignment as possible. > Could it be an RCR, or is just wrong-headed? :-) > > I want to assign into an object, but I want to take a proc as an arg, so > that the assignment can be "paramaterized" in some sense, like; > > # Usually: > card.email = "user@example.com" > > # Sometimes: > card.email = "user@example.com" do |e| > e.type = 'preferred' > e.protocol = 'uucp' > e.location = 'work' > end > > It doesn't seem to be allowed, I did a quick test: It's such a hybrid: it's a method call trying to look like an assignment trying to act like a method call. It thus ceases to be assignment emulation, so I don't think it would make sense. > I also tried to do a = method with two args, the second optional. > > It prints nothing, but runs, which makes me wonder what its doing! It's like: a = 1,2 # a == [1,2] a.x = 1, Proc.new # a.x= is called with arg of # [1, #] Again, the idea is to emulate assignment. In fact, look at this: irb(main):007:0> class C; def x=(y); @x = y; end; def x; @x; end; end # Yes I do know I've inlined an attr_accessor call :-) => nil irb(main):008:0> c = C.new => # irb(main):009:0> c.x,d = 1,2 #### This line in particular => [1, 2] irb(main):010:0> c.x => 1 irb(main):011:0> d => 2 David -- David A. Black dblack@wobblini.net