The Crystal Programming Language Forum

Need help with tuples

Having some trouble understanding this error message. How does one pass a tuple to a method?

def textfield(options)
 print options
end

tuple = {name: "textfield", required: true}
print tuple
#{name: "textfield", required: true}

textfield {name: "textfield", required: true}
#textfield {name: "textfield", required: true}
#                 ^
#Error: unexpected token: DELIMITER_START

The gist of it is that Crystal can’t differentiate the {} meaning a tuple or a block. The solution is to use () to make it more clear it’s an argument. I.e. textfield({name: "textfield", required: true}).

1 Like