Just for Fun: Break my code

0 views
Skip to first unread message

Matt Williams

unread,
Aug 28, 2007, 8:19:10 AM8/28/07
to colum...@googlegroups.com
The following code has issues.  Lots and lots of issues.  Every method has issues (some have volumes!!!).  In some methods each line can be passed data which will cause it to break (at least once).
It was an interesting exercise deliberately writing fragile code....  Now, can you invoke the various methods with arguments which will cause them to break?

Dude, break my code.

class Fragile
  attr_reader :name

  # Initially, we all start in the same place.... 
  # But we can get off to a bad start.
  def initialize(name)
    @name = name
  end
 
  # Slice it; dice it;
  # what causes the salad to shoot the wrong way?
  def caesar_salad()
    name.downcase.tr("abcdefghijklmnopqrstuvwxyz",
                     "zyxwvutsrqponmlkjihgfedbca")
  end
 
  # Change is hard.
  # original can be set with different values
  # such that errors can be caused by every
  # line
  def morph(original)
    s=original.upcase.gsub(/[^A-Z]/,"")
    s.tr!("ABC","123").squeeze!
    s.capitalize!.slice!(0,5)
    s=s[0].succ
  end
 
  # Everyone has their limit
  # what will cause me to break?
  def counter(limit)
    1.upto(limit){ |n| puts n}
  end
 
  # Shifting into gear; if you can't find 'em, grind 'em
  def red_shift(num,shift)
    (0 + num ) << shift
  end

  # If I'm not careful, I'll lose a sandal....
  # what will break me?
  def hermes(message)
    self.send(:sos,message.inspect)
  end
 
  # I don't break, but I have issues...
  def method_missing(symbol,*args)
    case symbol
      when :sos
      puts "I'll send an SOS to the world;\nMy message in a bottle is:\n#{arg[0]}"
    end
  end
 
end

--
I can say to myself and the world, "Look at all I am doing, am I not being busy? Am I not contributing? Am I not having an impact on all those around me and with whom I come into contact? See, my life has meaning."
To which the Tao responds, "You are doing, yes, but you are not being. Slow down, go with the flow, work with life, not against it. By being, you do. By doing, you cease to be."
Reply all
Reply to author
Forward
0 new messages