Questions tagged [instance-eval]

38 questions
23
votes
4 answers

Accessing Ruby Class Variables with class_eval and instance_eval

I have the following: class Test @@a = 10 def show_a() puts "a: #{@@a}" end class << self @@b = '40' def show_b puts "b: #{@@b}" end end end Why does following work: Test.instance_eval{show_b} b:…
haroldcampbell
  • 1,512
  • 1
  • 14
  • 22
9
votes
2 answers

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' …
Skiptomylu
  • 964
  • 1
  • 13
  • 21
8
votes
2 answers

class_eval vs instance_eval

Is there any difference in how class_eval & instance_eval work except def? Inside class_eval block def defines method to class itself (i.e. instance method) and inside instance_eval def defines method to the eigenclass of the class (i.e. class…
Alexey
  • 9,197
  • 5
  • 64
  • 76
7
votes
4 answers

Ruby instance_eval on a class with attr_accessor

I understand the basic difference between instance_eval and class_eval. What I've discovered though when playing around is something strange involving attr_accessor. Here's an example: A = Class.new A.class_eval{ attr_accessor :x } a = A.new a.x =…
brad
  • 31,987
  • 28
  • 102
  • 155
6
votes
1 answer

Ruby - Possible to pass a block as a param as an actual block to another function?

This is what I'm trying to do: def call_block(in_class = "String", &block) instance = eval("#{in_class}.new") puts "instance class: #{instance.class}" instance.instance_eval{ block.call } end # --- TEST EXAMPLE --- # This outputs…
Markus O'Reilly
  • 811
  • 3
  • 9
  • 15
5
votes
1 answer

Where is instance_eval defined in Ruby 1.9.2?

Forgive my ignorance, but I must be missing something here. I can find the documentation for instance_eval for ruby 1.8.7 in the Object class, but I just cannot find it anywhere for 1.9.2. I know the functionality is still supported since I'm using…
elmt
  • 1,604
  • 14
  • 24
5
votes
2 answers

Monkey patching built-in ruby classes in limited scopes

I'm working on an internal Ruby DSL and to make it look as pretty as possible I need to monkey patch the Symbol class and add some operators. I want to be responsible in how I do this and would like to limit the scope and lifetime of the patches to…
David K.
  • 6,153
  • 10
  • 47
  • 78
4
votes
3 answers

Ruby refinements with instance_eval

I'd like to provide some refinements to a DSL. I'm able to get refinements working with this example: module ArrayExtras refine Array do def speak puts 'array!' end end end module MyUniverse using ArrayExtras class Thing …
Andy
  • 285
  • 3
  • 12
4
votes
1 answer

Ruby Block Scope with instance_eval

My understanding of Ruby blocks and procs was that they are all closures. Now that I've seen it in use with instance_eval, I'm a little confused. What is the magic-sauce, the under workings when looking at the bare metal, that changes how a block's…
Chad M
  • 943
  • 1
  • 9
  • 22
3
votes
2 answers

Ruby: how does constant-lookup work in instance_eval/class_eval?

I'm working my way through Pickaxe 1.9, and I'm a bit confused by constant-lookup in instance/class_eval blocks. I'm using 1.9.2. It seems that Ruby handles constant-lookup in *_eval blocks the same way it does method-lookup: look for a definition…
Alan O'Donnell
  • 1,276
  • 9
  • 17
3
votes
2 answers

ruby access instance variable in instance_eval

I am trying some ruby metaprogramming and got some confusion with instance_eval(). see below examples @instance_var = 'instance_var' local_var = 'local_var' obj = Object.new obj.instance_eval { p @instance_var; p local_var } obj.instance_eval {…
3
votes
5 answers

Instance_eval does not work with do/end block, only with {}-blocks

If I have a class: class KlassWithSecret def initialize @secret = 99 end end and run: puts KlassWithSecret.new.instance_eval { @secret } it prints 99, but if I run: puts KlassWithSecret.new.instance_eval do @secret end It returns an…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
3
votes
1 answer

How does module_eval / class_eval / instance_eval counts the line numbers

I have found the line_number passed to class_eval, module_eval and instance_eval doesn't match the line numbers reported by the error. This behaviour is not explained by the ruby-doc which says: (use instance_eval as an example) the optional…
steveyang
  • 9,178
  • 8
  • 54
  • 80
2
votes
1 answer

Dynamically added accessor assignment doesn't work when invoking block via instance_eval in Ruby

I have a class to which I add attribute accessors dynamically at runtime. This class forms part a DSL, whereby blocks get passed to configuration methods and invoked using instance_eval. This makes it possible in the DSL to remove references to…
VirtualStaticVoid
  • 1,656
  • 3
  • 15
  • 21
2
votes
1 answer

Deep into Ruby class_eval and instance_eval

class_eval and instance_eval are quite predictable in such cases like defining methods. I also understand the difference between class's instance and class's singleton (aka eigenclass). BUT I cannot figure out the only thing like following: Let's…
bernstein7
  • 111
  • 5
1
2 3