From: dblack@... Date: 2006-10-02T04:37:36+09:00 Subject: Re: dynamic include ---2049402039-1754694657-1159731453=:21214 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1754694657-1159731453=:21214" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-1754694657-1159731453=:21214 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 2 Oct 2006, David Vallner wrote: > Florian A=DFmann wrote: >> To be complete here if the Exception: >> >> NameError: undefined local variable or method `role' for >> #> >> > > I smell a Ruby bug / gotcha. > > Apparently, in metaclass scope, the surrounding scope just isn't visible. The class keyword always creates a new scope, whether you give it a constant argument or a "<< obj"-style argument. > Try a workaround: > > person.roles.each do |role| > if role.class.const_defined?('Functions') > class << person > =09 self > end.class_eval do > include role.class.const_get('Functions') > end > end > end unless person.nil? You could also use extend, and avoid having to do the thing that won't be easy until RCR 231 is accepted :-) person.roles.each do |role| if role.class.const_defined?('Functions') person.extend role.class.const_get('Functions') end end David --=20 David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org ---2049402039-1754694657-1159731453=:21214-- ---2049402039-1754694657-1159731453=:21214--