Hello people, i am new at ruby and ror developing and i need your help
with this:
i got this code
class Trabajo
def saludar(nombre)
puts "Hola #{nombre}"
yield (("Fulano,32", "lol"))
end
end
class Trabajo2 < Trabajo
end
tr=Trabajo2.new
tr.saludar(“David”){ |nombre2, edad| puts “algo raro #{nombre2}, tu edad
es: #{edad}” }
ok, it is easy but i always got the same error: syntax error, unexpected
‘,’ , expecting ‘)’ could some of you help me, i think the code it’s
ok.
I would be glad for your help…
use:
yield [“Fulano,32”, “lol”]
yield wants only one object
Hans M. wrote in post #1060382:
use:
yield [“Fulano,32”, “lol”]
yield wants only one object
OMG, it was so easy, thank you very much
but why these codes work?
def a_method(a, b)
a + yield(a, b)
end
a_method(1, 2) { |a, b| (a + b) * 3 }
On Fri, May 11, 2012 at 2:37 PM, Hans M.
[email protected]wrote:
–
Best Regards,
*Larry Lv
*
its the " " …
yield("Fulano,32", "lol") # works
yield ("Fulano,32", "lol") # works not
mickey
6
On Fri, May 11, 2012 at 8:37 AM, Hans M. [email protected]
wrote:
use:
yield [“Fulano,32”, “lol”]
yield wants only one object
Sorry but this is plain wrong.
irb(main):001:0> def f; yield 1; yield 11,22; yield 111,222,333; end
=> nil
irb(main):002:0> f {|*a| p a}
[1]
[11, 22]
[111, 222, 333]
=> [111, 222, 333]
irb(main):003:0> f {|a| p a}
1
11
111
=> 111
irb(main):004:0> f {|a,b,c| printf “a=%p b=%p c=%p\n”, a, b, c}
a=1 b=nil c=nil
a=11 b=22 c=nil
a=111 b=222 c=333
=> nil
yield is like a method call (apart from the block you cannot pass).
Kind regards
robert
Hi,
David A. wrote in post #1060379:
Hello people, i am new at ruby and ror developing and i need your help
with this:
i got this code
class Trabajo
def saludar(nombre)
puts "Hola #{nombre}"
yield (("Fulano,32", "lol"))
end
[…]
ok, it is easy but i always got the same error: syntax error, unexpected
‘,’ , expecting ‘)’ could some of you help me, i think the code it’s
ok.
I would be glad for your help…
The expression ((“Fulano,32”, “lol”)) isn’t valid Ruby. What did you
want to do?
If you wanted to put the block arguments in paranthesis, you need
single paranthesis directly after the “yield” (without any space):
yield(“Fulano,32”, “lol”)
But you can actually leave out the paranthesis.