This works
irb(main):001:0> name = \"Rohit \" \"Sharma\"
=> \"Rohit Sharma\"
But this doesn\'t
irb(main):001:0> fn
Just put a +
inbetween them like
name = fname + lname
string + string
is defined to return a new string containing the two inputs concatenated together.
The error is related to the fact that fname
would have to be a function for this to work. Instead, try
name = fname + lname
or even
name = "#{fname}#{lname}"
but where you had
name = "Rohit " "Sharma"
it is a special case, since Ruby will join the two strings automatically.
When you do
name = "Rohit " "Sharma"
You don't create two String
s objects that then merge together to create one string. Instead, the Ruby (interpreter/compiler/whatever) looks at the code, and merges it together before producing a single String
object.
So you can do
name = "Rohit " "Sharma"
but not
first_name_plus_space = "Rohit "
last_name = "Sharma"
name = first_name_plus_space last_name