0

I am a beginner in Ruby. Could someone help me with this? I want to call a method within ""

def callme
"this string"  # this is usually a path that I want to source from a single location 
end               


v= "string + callme "

how do I call the function within quotes here? The quotes are essential for my code.

Zazy
  • 47
  • 6
  • When you say *"The quotes are essential for my code"* can you explain? Is the intended result `"string + this string"` or `"string this string"` or `"string + \"this string\""`, etc. as of right now it is very unclear. BTW as a minor grammatical suggestion there are no *functions* in ruby, only *methods* – engineersmnky Aug 30 '21 at 15:25

2 Answers2

3

You can use string interpolation / concatenation:

def hi
  'Hello'
end

def world
  'World'
end

# Concatenation
hi + ' ' + world   #=> "Hello World"

# Interpolation
"#{hi} #{world}"   #=> "Hello World"

See this answer for more details

Stefan
  • 109,145
  • 14
  • 143
  • 218
Sumak
  • 927
  • 7
  • 21
0

If I understand correctly, what you are looking for is string interpolation. The syntax for this is #{...}, like so:

v= "string + #{callme} "

This sets the variable v to "string + this string ".

Reference

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62