473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Metaprogramming in Ruby: class_eval vs. instance_eval

I'm using Ruby 1.8.6 btw.

For the longest time today (it seems) I've been trying to understand exactly the operating difference between class_eval and instance_eval. There are some obvious differences I'm perfectly aware of (e.g. class_eval is only on Class objects), but the reality is that some irb tinkering has left me more confused than ever. Given the following:

Expand|Select|Wrap|Line Numbers
  1. class Foo
  2.   # could be any class
  3. end
  4.  
  5. f = Foo.new
  6.  
  7. # Try out instance eval on f's class (Foo)
  8. f.class.instance_eval do
  9.   define_method :made_by_inst_eval do
  10.     puts "This method was made by calling instance_eval"
  11.     puts "Current 'self': #{self}"
  12.     puts "Current class of 'self': #{self.class}"
  13.   end
  14. end
  15.  
  16. f.made_by_inst_eval
  17.  
Gives me this:

Expand|Select|Wrap|Line Numbers
  1. This method was made by calling instance_eval
  2. Current 'self': #<Foo:0x8f098>
  3. Current class of 'self': Foo
If I then try doing the exact same thing with class_eval instead:

Expand|Select|Wrap|Line Numbers
  1. f.class.class_eval do
  2.   define_method :made_with_class_eval do
  3.     puts "This method was made by calling class_eval"
  4.     puts "Current 'self': #{self}"
  5.     puts "Current class of 'self': #{self.class}"
  6.   end
  7. end
  8.  
  9. f.made_with_class_eval
  10.  
I then get the same damn result:

Expand|Select|Wrap|Line Numbers
  1. This method was made by calling class_eval
  2. Current 'self': #<Foo:0x8ec88>
  3. Current class of 'self': Foo
  4.  
So, what's the difference between these two calls? In this particular situation? In general?

Some clear thoughts would be ever so appreciated!
Sep 7 '08 #1
1 13622
improvcornartist
303 Expert 100+
There may not be much of a difference in this situation, since both times it is being called on f.class. I think the difference between the two is the receiver of where the method is defined, and thus what it applies to. This page may provide a little extra info around example 5.
Sep 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
3
by: wakun | last post by:
Hi there, I am working a project in numerical computation in which iterative method is applied for solving equation. The problem is so big and slow. Few days ago, I found a paper on...
9
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
9
by: andrew cooke | last post by:
Hi, Thanks for the help a couple of days ago. I completed what I was doing and wrote a summary which I've posted at http://acooke.org/cute/PythonMeta0.html (it's kind of long to post here). I...
16
by: Wilson | last post by:
Hi all, I have an interesting problem that I'm hoping can be solved with metaprogramming, but I don't know how far Python supports code generation (and I don't know if I'm taking the correct...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.