# String -\> symbol, redux

**URL:** <https://www.ruby-forum.com/t/string-symbol-redux/104846>\
**Category:** Ruby\
**Created:** [May 23, 2007, 5:06am UTC](https://www.ruby-forum.com/t/string-symbol-redux/104846 "2007-05-23T05:06:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 23, 2007, 5:06am UTC](https://www.ruby-forum.com/t/string-symbol-redux/104846/1 "2007-05-23T05:06:00Z")

</div>

ok… i’m still not getting it. i want the elegant way to do the last  
two lines in my Server initialize. and please be kind - i’m just a  
programming hack, not a professional. =)

* * *

module Toc44\_all

def toc44\_all\_system(cmd)  
the\_pipe=IO.popen(cmd)  
res = the\_pipe.read  
the\_pipe.close  
return res  
end

def toc44\_all\_twoargs(a,b)  
return “#{a} - #{b}”  
end

end

module Toc44\_fw  
def toc44\_fw\_justme(arg)  
return “returning #{arg}”  
end  
end

class Server  
def initialize  
my\_type = “fw”  
extend eval(“Toc44\_all”)  
extend eval(“Toc44\_#{my\_type}”)  
eval(“def toc44\_fw\_system(args); return toc44\_all\_system(args);end”)  
eval(“def toc44\_fw\_twoargs(\*args); return  
toc44\_all\_twoargs(\*args);end”)  
end  
end

svr = Server.new  
puts svr.toc44\_fw\_justme(“howdy”)  
puts svr.toc44\_fw\_system(“dir”)  
puts svr.toc44\_fw\_twoargs(“hi”,“there”)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 23, 2007, 11:24am UTC](https://www.ruby-forum.com/t/string-symbol-redux/104846/2 "2007-05-23T11:24:30Z")

</div>

On Wed, May 23, 2007 at 12:05:31PM +0900, removed\_email\_address@domain.invalid wrote:

> ok… i’m still not getting it. i want the elegant way to do the last two lines in my Server initialize. and please be kind - i’m just a programming hack, not a professional. =)  
> …  
> class Server  
> def initialize  
> my\_type = “fw”  
> extend eval(“Toc44\_all”)  
> extend eval(“Toc44\_#{my\_type}”)  
> eval(“def toc44\_fw\_system(args); return toc44\_all\_system(args);end”)  
> eval(“def toc44\_fw\_twoargs(\*args); return toc44\_all\_twoargs(\*args);end”)  
> end  
> end

Well,

class Server  
def initialize  
my\_type = “fw”  
extend Toc44\_all  
extend Object.const\_get(“Toc44\_#{my\_type}”)  
class \<\<self  
alias :toc44\_fw\_system :toc44\_all\_system  
alias :toc44\_fw\_twoargs :toc44\_all\_twoargs  
end  
end  
end

I’m not sure exactly what you’re trying to achieve, but there’s almost  
certainly a better way of doing it.

For example, if the idea is to have multiple Server objects, with  
different  
behaviour for certain groups of devices, I’d say have a separate class  
for  
each type of server and instantiate the correct one dynamically.

module Server  
class Firewall  
def system  
puts “Howdy”  
end  
end

class Router  
def system  
puts “Flurble”  
end  
end  
end

klass = “Firewall”  
Server.const\_get(klass).new.system

klass = “Router”  
Server.const\_get(klass).new.system

Then any code which is common to Firewall and Router classes can be put  
in a  
module (i.e. “include Foo” inside each class), or you can make them  
subclasses of a common ancestor class.

Sticking them inside a module namespace (‘Server’ in the above) prevents  
you  
accidentally instantiating any object outside of that namespace. For  
example, if you have a knotted string reader and writer, then  
Server.const\_get(“String”).new  
can only create an instance of Server::String, not a String.

HTH,

Brian.
