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

S

Sam Roberts

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 = "(e-mail address removed)"

# Sometimes:
card.email = "(e-mail address removed)" 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
 
G

gabriele renzi

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 = "(e-mail address removed)"

# Sometimes:
card.email = "(e-mail address removed)" 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..
 
D

David A. Black

Hi --

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 = "(e-mail address removed)"

# Sometimes:
card.email = "(e-mail address removed)" 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, #<Proc:0000....>]

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
=> #<C:0x401c7434>
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
 
S

Sam Roberts

Quoteing (e-mail address removed), on Mon, Nov 15, 2004 at 06:07:10AM +0900:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top