Zazy
Zazy

Reputation: 97

Call a function within Quotes " "

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.

Upvotes: 0

Views: 138

Answers (2)

Sumak
Sumak

Reputation: 1051

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

Upvotes: 3

Wander Nauta
Wander Nauta

Reputation: 19625

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

Upvotes: 0

Related Questions