I’m writing a ruby method that takes a string input but I don’t want to type the quotes.
For example:
def noquotes(input)
puts input
end
noquotes('12Dec11Bel01') # ---> 12Dec11Bel01
noquotes(12Dec11Bel01) # ---> Currently yields an error
What I’d like to be able to do is enter the method input without the quotes (second example) and still get the right result. I tried using .to_str to ensure the input was treated as a string, but it didn’t work.
Hehe, sorry, but you can’t mangle with the syntax tree in Ruby. If you don’t make quotes, it will be parsed as a variable or method call.
What you can do is
but use that wisely and with scoping, as in
Use with caution and only if you know what you’re doing! And only in DSL. And not even there. Really. Don’t do it.