Lotto checking script

Posted by root Tue, 12 Feb 2008 19:16:00 GMT

The idea comes from http://www.waider.ie/hacks/workshop/perl/lotto.pl script.

Should I be so wasteful? No:

irb
my_numbers       = [1,2,3,4,5,6]
winning_numbers = [1,2,3,41,42,43]

result = 6 - (winning_numbers - my_numbers).size

puts "#{result} " + 
  if not result.eql? 0
    "hits"
  else
    "hit"
  end

Posted in ,  | Tags  | no comments

String Iterator

Posted by root Mon, 14 Jan 2008 22:08:00 GMT

class String
  class Iterator < String

    def initialize(str='', next_char=0)
      @next_char = next_char
      super(str)
    end

    def next
      return nil if @next_char > self.size

      @next_char += 1

      get_char(-1)
    end

    def prev
      return nil if @next_char <= 0

      @next_char -= 1 if @next_char > 0

      get_char()
    end

    private
    def get_char(pos=0)
      if block_given?
        yield split(//)[@next_char+(pos)]
      else
        split(//)[@next_char+(pos)]
      end
    end
  end
end

s = String::Iterator.new "abcdef"

while char = s.next
  puts char
end

while char = s.prev
  puts char
end

Posted in  | Tags ,  | no comments