Something in Ruby

Posted in Programming, Ruby on May 30, 2008

#!/usr/bin/env ruby

String.class_eval {
  define_method(:translate_to) do |lang|
    text = case lang
           when :spanish
           "Buenos Dias, Como estas hoy?"
           when :english
           "Good Morning, How are you today?"
           else
           "I don't speak any other language."
           end
  end
}

puts "Sentence".translate_to :spanish
# => Prints out "Buenos Dias, Como estas hoy?"

puts "Sentence".translate_to :english
# => Prints out "Good Morning, How are you today?"

puts "Setenece".translate_to :portuguese
# => Prints out "I don't speak any other language."

puts "Sentence"
# => Prints out "Sentence"

So while messing round I made this in Ruby… I’m not sure if it is considered meta programming? Well, it’s just adding a method to the String class. I get confused when to consider things to be meta programming.. Maybe I over think about it.

Trackback URI | Comments RSS

One Response to “ Something in Ruby ”

  1. # 1 DennisBB Says:

    kool

Leave a Reply

  • Recent Comments

    • Ahmed: @fernyb.. Thanks man! I forked the project, but I am newbie in open source, and don’t know where to...
    • fernyb: @Ahmed, yeah you can help. It’s hosted on (http://github.com/fernyb/Cupc ake) so you can also fork it...
    • Ahmed: And hey! Can I help with your new PHP framework? plssssssssss :( (
    • Ahmed: hmmmmm I just wanna ask you about something… did you ever create API for Craigslist? I am thinking of...
    • Matt James: Same here… amazing project that I have been very happily using for a while. Sadly it seems to be...

    Recent Listened to Tracks

    Loading...

Bad Behavior has blocked 66 access attempts in the last 7 days.

eastern-avalanche
eastern-avalanche
eastern-avalanche
eastern-avalanche