[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I guess "obj.attr = arg" methods don't allow blocks... (maybe rcr?

Sam Roberts

11/14/2004 7:48:00 PM


Does this conflict with some kind of basic rule in the grammer?

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:

class Foo
def bar=(arg)
yield "got: #{arg}"
end
end

f = Foo.new

# Syntax error
f.bar = "hi do end" do |s|
puts "do/end #{s}"
end


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!

class Foo
def bar=(arg, blk = nil)
if blk
blk.call("got: #{arg}")
end
end
end

f = Foo.new

f.bar = "hi bare"

f.bar = " hi do end", proc { |s| puts "do/end #{s}" }


Cheers,
Sam



3 Answers

gabriele renzi

11/14/2004 7:56:00 PM

0

Sam Roberts ha scritto:

> Does this conflict with some kind of basic rule in the grammer?
>
> 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:

I don't know what's going on, but I'd ask:
why don't you use an Email class and write:
card.email = Email.new('user@foo') {block}

it seem much more clean to me..

dblack

11/14/2004 9:07:00 PM

0

Sam Roberts

11/14/2004 9:39:00 PM

0

Quoteing dblack@wobblini.net, on Mon, Nov 15, 2004 at 06:07:10AM +0900:
> 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.

And I'm trying to use an operator in a way that acts closer to a method,
the opposite of the intention, then.

Thanks for the explanation, I'll do this with a real method.

Cheers,
Sam