Function object
In Ruby, several objects can be considered function objects, in particular Method and Proc objects. Ruby also has two kinds of objects that can be thought of as semi-function objects: UnboundMethod and block. UnboundMethods must first be bound to an object (thus becoming a Method) before they can be used as a function object. Blocks can be called like function objects, but to be used in any other capacity as an object (e.g. passed as an argument) they must first be converted to a Proc. More recently, symbols (accessed via the literal unary indicator codice_35) can also be converted to codice_36s. Using Ruby's unary codice_37 operator—equivalent to calling codice_38 on an object, and assuming that method exists—the Ruby Extensions Project created a simple hack.
ERuby
The second code snippet changes to the below code:
string = "The value of x is: #{x}"
x = 400
puts string
The above code will not get executed. This is because the 1st line does not know the value of x when it gets executed. Thus, the main reason of using an ERB object is to write templates ahead of time, by "binding" variables and methods which may not exist at the given time. The template gets processed only when "result" is called on the ERB object. In order to get access to instance methods and instance variable of an object, ERB makes use of a "binding" object. Access to variables and methods of an object is given by the private "binding" object which exists in each ruby class. It is easy to get access to methods and variables within the method of a class. But to access variables of a different class, that class will have to expose its binding object via a public method. The example is as shown below:
class ERBExample