سؤال

Can someone explain why this is wrong?

object = Special::MyObject.new 'string' { "a block" }

I get a SyntaxError on

.. { "a ...
    ^

It expects a keyword_end

If I remove the block everything works fine

هل كانت مفيدة؟

المحلول

You can't use brace block ({ ... }) with methods with no parentheses. You can use do ... end or add ( ... ).

object = Special::MyObject.new('string') { "a block" }

object = Special::MyObject.new 'string' do
  "a block"
end

نصائح أخرى

object = Special::MyObject.new('string') { "a block" }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top