Ruby - Call method in String interpolation

Introduction

Escape characters such as \n and \t represents a newline and a tab.

A single-quoted string does no such evaluation.

A single-quoted string can, however, use a backslash to indicate that the next character should be used literally.

This is useful when a single-quoted string contains a single-quote character, like this:

Demo

puts 'It\'s my party' 


class MyClass # from  ww w .  j a v  a 2 s.  co m
    attr_accessor :name 
    attr_accessor :number 
                         
    def initialize( aName, aNumber ) 
        @name    = aName 
        @number = aNumber 
    end 
    def returnNumber
      return 123
    end                         
end 

ob = MyClass.new( "Java", "007" ) 

puts( "A tab\ta new line\na calculation #{2*3} and method-call #{ob.returnNumber}" )

Result

Related Topic