'Ruby: how to interpolate in a string? [closed]

Is a simple question about how to print an interpolation on a string in ruby My method is this:

def hello(parameter)
  puts "hello + #{parameter}"
end

puts hello(name) but irb, show me these message: NameError: undefined local variable or method `name' for main:Object

It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.

Thank you, it's my first post ejje.



Solution 1:[1]

The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tani Nevins-Klein