From: David Alan Black Date: 2001-11-06T03:08:50+09:00 Subject: [ruby-talk:24409] Re: Help with OO H3ll0 -- On Tue, 6 Nov 2001, Vincent Foley wrote: > 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'? Well, this is sort of a variant of what you're asking for, but it was fun to write :-) module L33T # Slightly ineff[ective,icient] for inverting, but anyway TRANSLATIONS = { 'Ee' => '33', 'Aa' => '44', 'Ii' => '11', 'Oo' => '00', 'Tt' => '77', 'S' => '5', 's' => '$' } def translate(table) table.each do |from,to| tr!(from,to) end self end def uncool_to_cool translate(TRANSLATIONS) end def cool_to_uncool translate(TRANSLATIONS.invert) end end class L33T57R1ng < String include L33T def initialize(s) super uncool_to_cool end end if __FILE__ == $0 s = L33T57R1ng.new("Hello, most excellent dude") p s p s.cool_to_uncool end David -- D4v1d 4l4n Bl4ck h0m3: dbl4ck@c4ndl3.$up3rl1nk.n37 w0rk: bl4ckd4v@$hu.3du W3b: h77p://p1r473.$hu.3du/~bl4ckd4v