Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby difference between send and instance_eval?

I know send takes string or symbol with arguments while instance_eval takes string or block, and their difference could be apparent given receivers.

My question is what the 'under the hood' difference is for the example below?

1234.send 'to_s'               # '1234'
1234.instance_eval 'to_s'      # '1234'
like image 358
Skiptomylu Avatar asked Aug 04 '13 20:08

Skiptomylu


People also ask

What is send in Ruby?

send is a Ruby method allowing to invoke another method by name passing it any arguments specified.

What is instance_ eval in Ruby?

instance_eval(array_second) adds the method second to the variable a — which is an instance of Array — by passing a String that will be evaluated in the context of the instance a . The call to str. instance_eval with a block will evaluate the content of the block in the context of str — which is an instance of String .


2 Answers

From the fine manual:

send(symbol [, args...]) → obj
send(string [, args...]) → obj

Invokes the method identified by symbol, passing it any arguments specified. [...] When the method is identified by a string, the string is converted to a symbol.

and for instance_eval:

instance_eval(string [, filename [, lineno]] ) → obj
instance_eval {| | block } → obj

Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables.

So send executes a method whereas instance_eval executes an arbitrary block of code (as a string or block) with self set to the object that you're calling instance_eval on.

In your case, there isn't much difference as the string you're handing to instance_eval is just a single method. The main difference is that anyone reading your code (including you in six months) will be wondering why you're using instance_eval to call a single method.

You might also be interested in Object#public_send and BasicObject#__send__

like image 120
mu is too short Avatar answered Sep 28 '22 08:09

mu is too short


Whatever you can do with send is a proper subset of that of instance_eval. Namely, the argument to send has to be a single method (and its arguments), whereas the argument to instance_method is an arbitrary code. So whenever you have send, you can rewrite it with instance_eval, but not vice versa.

However, performancewise, send is much faster than instance_eval since there is no additional parsing required to execute send, whereas instance_eval needs to parse the whole argument.

In your example, the result will be the same, but the first one will run faster.

like image 20
sawa Avatar answered Sep 28 '22 09:09

sawa