From: Armin Roehrl Date: 2001-11-06T02:54:32+09:00 Subject: [ruby-talk:24406] Re: Help with OO > Hi, I'm a bit new to OO programming and I would need help. I made a > small (and useless) program to convert a string to l33t speech. But the > method I used, I think it's a bit too imperative (we're in a OO > language, right?). So here's the code, and I'll explain what I want to > do after the code: > > [code] > > #!/usr/bin/env ruby > > def l33t(message) > message.tr!('eE', '3') > message.tr!('aA', '4') > message.tr!('lL', '1') > message.tr!('oO', '0') > message.tr!('tT', '7') > message.tr!('sS', '$5') > > return message > end > > print "Enter string to translate: " > x = STDIN.gets.chomp! > puts l33t(x) > > [/code] > > look at the last line, how would I do to do 'puts x.l33t'? I think you have different ways of doing this. (a) One way would be to define your own String class with the extra method l33t; class MyString < String def intitialize(s) super(s) end def l33t self.tr!('eE', '3') self.tr!('aA', '4') self.tr!('lL', '1') self.tr!('oO', '0') self.tr!('tT', '7') self.tr!('sS', '$5') self end end print "Enter string to translate: " x =MyString.new(STDIN.gets.chomp!) puts x.l33t (b) simply extend l33t to x x = STDIN.gets.chomp! class << x def l33t self.tr!('eE', '3') self.tr!('aA', '4') self.tr!('lL', '1') self.tr!('oO', '0') self.tr!('tT', '7') self.tr!('sS', '$5') self end end puts x.l33t Enjoy, A ----------------------------------------------- Armin Roehrl, http://www.approximity.com "The best way to predict the future is to invent it." --Alan Kay