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
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