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 #  ww  w  .  java  2s.c om
    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