Skip to content

Instantly share code, notes, and snippets.

@igrep
Created August 24, 2010 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrep/546900 to your computer and use it in GitHub Desktop.
Save igrep/546900 to your computer and use it in GitHub Desktop.
Wacky Ruby
#!/usr/bin/ruby -wKu
# vim: set fileencoding=utf-8 :
class ObjectPattern
def initialize obj, method=:[]
@delegate_object = obj
@delegate_method = method
end
attr_reader :delegate_object, :delegate_method
def === other
@delegate_object.__send__( @delegate_method, other )
end
end
module Kernel
module_function
def P *args #Can anyone make up a good name?
ObjectPattern.new( *args )
end
def it method, obj
ObjectPattern.new obj, method
end
end
if $PROGRAM_NAME == __FILE__
print 'Enter Integers ( 1-99 ): '
while input = gets
case input.to_i
when (it :>, 1 )
puts 'Extra Small'
when (it :>=, 33 )
puts 'Small'
when (it :>=, 66 )
puts 'Midium'
when (it :>=, 99 )
puts 'Large'
else
puts 'Extra Large'
end
end
end
#!/usr/bin/ruby -Ku
# vim: set fileencoding=utf-8 :
=begin
_ argument like scala's function
=end
module ProcWithBar
BAR_ARGS_ = []
module_function
def _ &block
if block_given?
Proc.new {|*args|
BAR_ARGS_.replace args
val = block.call( *args )
BAR_ARGS_.clear #Clear entries that is not used by the +block+.
val
}
else
raise ArgumentError, 'too many arguments' if BAR_ARGS_.empty?
BAR_ARGS_.shift
end
end
end
if $PROGRAM_NAME == __FILE__
require 'object-pattern'
include ProcWithBar
puts( (1..10).inject( &_{ _+_ } ))
print 'Enter Integers ( 1-99 ): '
while input = gets
case i = input.to_i
when P( _{ ( _ % 15 ).zero? } )
puts 'FizzBuzz'
when P( _{ ( _ % 3 ).zero? } )
puts 'Fizz'
when P( _{ ( _ % 5 ).zero? } )
puts 'Buzz'
else
puts i
end
end
##NEED MORE TESTS!!###
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment