[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: class_eval(aString) vs class_eval(aBlock)
From: Gioele Barabucci <ml () gioelebarabucci ! com>
Date: 2006-01-24 12:19:32
Message-ID: 200601241313.55705.ml () gioelebarabucci ! com
[Download RAW message or body]
Hi,
why does class_eval behave differently is you pass a string or a block to it?
This simple test
| class A
| def test_str; p self.class.class_eval("@@v"); end
| def test_blk; p self.class.class_eval { @@v }; end
| end
|
| class B < A
| def initialize; @@v = "v.B"; end
| end
|
| B.new.test_str
| B.new.test_blk
gives this (unexpected?) result
| $ ruby ./cv2.rb
| "v.B"
| ./cv2.rb:3:in `test_blk': uninitialized class variable @@v in A (NameError)
| from ./cv2.rb:3:in `class_eval'
| from ./cv2.rb:3:in `class_eval'
| from ./cv2.rb:3:in `test_blk'
| from ./cv2.rb:11
--
Gioele <dev@gioelebarabucci.com>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic