564 views
What's the return value of the following Ruby code?
class Hi def initialize(name) @name = name end def to_s = "Hi #@name" end Hi.new("RubyCademy").to_s # => ???
The correct answer is
"Hi #@name"
"Hi RubyCademy"
nil
#❮Hi:0x13005 @name="RubyCademy"❯
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
Don't miss out on our special offer!
This is the perfect chance to take advantage of exclusive content, quizzes, and expert insights that will take your development journey to the next level.
Instance variable in Ruby store data associated with an instance of a class.
A common task is embedding their values within strings, known as interpolation.
Ruby offers multiple ways to achieve string interpolation; in this guide, we’ll explore a special form using instance variables directly, as illustrated by the following example.
class Hi def initialize(name) @name = name end def to_s = "Hi #@name" end Hi.new("RubyCademy").to_s # => "Hi RubyCademy"
This example demonstrates a Ruby class, Hi, with an instance variable @name and a method that returns a string interpolating this instance variable.
#{}
and #@ivar
Typically, in Ruby, string interpolation is done using the #{} syntax.
For example:
def to_s "Hi #{@name}" end
However, there is a shorthand for interpolating instance variables directly, which is what the example uses: #@ivar
.
This eliminates the need for the curly braces when you are interpolating instance variables:
def to_s = "Hi #@name"
In this case, #@name
directly interpolates the value of the @name
instance variable into the string.
When Hi.new("RubyCademy").to_s
is called, Ruby evaluates the expression inside the string and replaces #@name
with the value "RubyCademy"
, returning "Hi RubyCademy"
.
#@ivar
vs #{@ivar}
While both forms of interpolation achieve the same result, let’s compare them:
class Hi def to_s_with_braces "Hi #{@name}" # Using #{} end def to_s_without_braces "Hi #@name" # Using #@ivar end end
#{}
)This is the most common and flexible form of string interpolation. You can interpolate any expression, not just instance variables.
#@ivar
)This is a shorthand, limited to interpolating instance variables directly.
The shorthand is a bit more concise, but you might prefer #{}
for consistency or when interpolating more complex expressions.
Readability: The #@ivar
form might be confusing to developers who aren’t familiar with this Ruby shorthand.
For the sake of clarity, you may want to stick with the #{}
form, especially in shared codebases.
Limited to Instance Variables: The #@ivar
form only works with instance variables.
If you want to interpolate local variables, method calls, or expressions, you’ll need to use #{}
.
Ruby provides multiple ways to achieve string interpolation. The #@ivar
shorthand is a convenient method to interpolate instance variables directly, but its use is generally limited to simple cases.
For more complex interpolations or better readability, the #{}
syntax is usually preferred.
Voilà!
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
RubyCademy ©