1

Here is class foo

class Foo
  def initialize(&block)
    instance_eval(&block) if block
  end

  def bar(hash)
    @bar = hash
  end
end

why is this syntax error

foo = Foo.new do
  bar {:param1=>'value1', :param2=>"value2"}
end

the syntax error this gives me is SyntaxError: unexpected ',', expecting '}' bar {:param1=>'value1',:param2=>'value2'} whats the proper way to use hash inside instance_eval like the example above. What i really need is to pass in hash to a method inside instance_eval

4
  • 2
    Add parentheses and you're good to go: bar({:param1=>'value1', :param2=>"value2"}). As it is, {:param1=>'value1', :param2=>"value2"} is treated as block rather than an argument. Commented Oct 24, 2016 at 5:20
  • thanks you solved my problem Commented Oct 24, 2016 at 5:23
  • 1
    You can alternatively write bar :param1=>'value1', :param2=>"value2"or bar param1: 'value1', param2: "value2" (or the same with parentheses). Commented Oct 24, 2016 at 5:27
  • thanks a lot now it will feel less ugly without those parenthesis. Commented Oct 24, 2016 at 6:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.