Ruby - Using embedded evaluation in double-quoted strings

Introduction

The following code shows an example of embedded evaluation in double-quoted strings.

For example, here we have created an object, ob, from a custom class, MyClass, and used embedded evaluation to display the values of its name and number attributes:

Demo

class MyClass 
    attr_accessor :name # w  w  w. ja  va 2 s  .  c o m
    attr_accessor :number 
                         
    def initialize( aName, aNumber ) 
        @name    = aName 
        @number = aNumber 
    end 
                         
end 

ob = MyClass.new( "Java", "007" ) 
puts( "My name is #{ob.name} and my number is #{ob.number}" )

Result

Related Topic