1

Ruby doesn't like this:

item (:name, :text) {
  label('Name')
}

And I don't know why. I'm attempting to create a DSL. The 'item' method looks like this:

def item(name, type, &block) 
  i = QbeItemBuilder.new(@ds, name, QbeType.gettype(type))
  i.instance_exec &block
end

Take a name for the item, a type for the item, and a block. Construct an item builder, and execute the block in its context.

Regardless of whether or not I need to use instance_exec (I'm thinking that I don't - it can be stuffed in the initialiser), I get this:

SyntaxError (ds_name.ds:5: syntax error, unexpected ',', expecting ')'
  item (:name, :text) {
              ^

How do I invoke method with multiple arguments and a block? What does ruby think I'm trying to do?

2 Answers 2

3

The space before parentheses is causing ruby to evaluate (:name, :text) as single argument before calling the method which results in a syntax error. Look at these examples for illustration:

puts 1      # equivalent to puts(1)       - valid
puts (1)    # equivalent to puts((1))     - valid
puts (1..2) # equivalent to puts((1..2))  - valid
puts (1, 2) # equivalent to puts((1, 2))  - syntax error
puts(1, 2)  # valid

Your way of providing the block is syntactically valid, however when the block is not in the same line as the method call it is usually better to use do ... end syntax.

So to answer your question you can use:

item(:name, :text) { label('Name') }

or:

item(:name, :text) do
  label('Name')
end
Sign up to request clarification or add additional context in comments.

1 Comment

Arrgh! Arrgh! It's attempting to evaluate the parenthesed but as an expression! Thanks, man.
2

Remove the space before the ( in item (:name, :text) {

1 Comment

Not supposed to put a space between the parenthesis and the argument.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.