From: Robert Klemme on
2009/11/18 Alexandre Mutel <alexandre_mutel(a)yahoo.fr>:
> Robert Klemme wrote:
>> Personally I find that the more interesting question.  Do you want to
>> have the side effect of updating potentially many classes (and objects
>> via their class and #extend)?  Maybe there is a better design choice?  I
>> don't know your use case or what you need that behavior for.  Generally
>> Matz pics _very_ reasonable choices so I tend to assume that the
>> aforementioned side effect is usually not wanted.  Which does not mean
>> that there is no use case for this.
>
> Thanks for you response! You are right. I have resolved this without
> using this trick and using plain .extends/include as you mentioned.
>
> My use case is simple : i would like to develop a DSL language that
> provides sub-DSL language that you can switch at runtime.
>
> For example, the top DSL is inside module ALang and i have two sub
> language extension in ALang_B and ALang_C like this:

<snip/>

> Do you any project that use sub DSL language loaded/unloaded like this?

No, and I'd rather resort to a different approach, e.g.


module ALang_B
def myABFunction()
puts "myAB"
end
end

module ALang_C
def myACFunction()
puts "myAC"
end
end

module ALang
@langs = {:B => ALang_B, :C => ALang_C}
def self.lang(x) @langs.fetch(x) end

def use(sublang)
# printf "use %p\n", self
@lg = Object.new.extend(ALang.lang(sublang))
end

def self.included(x)
# printf "included %p %p %s %p\n", self, x, x.to_s, x.class
end


def method_missing(*a,&b)
# printf "missing %p\n", self
if @lg
@lg.send(*a,&b)
else
super
end
end
end

include ALang

use :B
myABFunction

begin
myACFunction
rescue => e
puts e
end

use :C
# myABFunction should not be usable
myACFunction

begin
myABFunction
rescue => e
puts e
end

Or pick a completely different approach where use receives a block in
which you can use the particular language, e.g.

module ALang

def use(sublang)
# printf "use %p\n", self
@lg = Object.new.extend(ALang.lang(sublang))
end
end

use2 :B do
myABFunction

begin
myACFunction
rescue => e
puts e
end
end

use2 :C do
# myABFunction should not be usable
myACFunction

begin
myABFunction
rescue => e
puts e
end
end

Kind regards

robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Alexandre Mutel on
Robert Klemme wrote:
> No, and I'd rather resort to a different approach, e.g.

Woo, thanks Robert, this is a really clean approach!
--
Posted via http://www.ruby-forum.com/.