Metaprogramming with Darkie The Fridge

Posted on July 12, 2006


m = "mr_beastly_"
(1..50).collect { |darkie_the_fridge|
  thefun = m + darkie_the_fridge.to_s + "!"
  eval %{
    def #{thefun}(cinnamocha)
      puts cinnamocha * #{darkie_the_fridge}
    end
  }
}
mr_beastly_5! 'boogiebot '

The result of this is boogiebot boogiebot boogiebot boogiebot boogiebot.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. JTAxx Wed, 12 Jul 2006 14:11:02 UTC

    how would you call the function from the created array, like

    a = (1..50).collect {………..}

    then if you’d call a[5] ‘boogiebot’, it would return the same output as mr_beastly_5! ?

  2. Administrator Wed, 12 Jul 2006 16:32:39 UTC

    Problem is, the collect has to return a proc or the assignment would yield nil. Consider the following example.

      m = 'mr_beastly_'
      funarray = (1..50).collect { |darkie_the_fridge|
        thefun = m + darkie_the_fridge.to_s + "!"
        gift_of_beard = "Proc.new { |cinnamocha| puts cinnamocha * #{darkie_the_fridge} }"
        eval %{ def #{thefun}(cinnamocha)
                  #{gift_of_beard}.call(cinnamocha)
                end
              }
        eval(gift_of_beard)
      }
      mr_beastly_6!('byzantine ghoulish pillary')
      funarray[6].call('zingosphere ')
      boogiebot = funarray[10]
      boogiebot['zazzle99 ']

    Output is

    byzantine ghoulish pillarybyzantine ghoulish pillarybyzantine ghoulish pillarybyzantine ghoulish pillarybyzantine ghoulish pillarybyzantine ghoulish pillary
    zingosphere zingosphere zingosphere zingosphere zingosphere zingosphere zingosphere
    zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 zazzle99 

    Also, try out variations with irb.

Comments