public
Created

SaaS-Class: Homework 2

  • Download Gist
cartesian.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class CartesianProduct
include Enumerable
def initialize(a,b)
@values = []
@values = a.product(b) unless b.empty?
end
def each(&block)
@values.each {|v| yield v}
end
end
 
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
 
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
currency.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.0}
def method_missing(method, *args)
singular_currency = (method == :in ? args.first : method).to_s.gsub(/s$/, '')
if @@currencies.has_key?(singular_currency)
self.send(method == :in ? :/ : :*, @@currencies[singular_currency])
else
super
end
end
end
 
class String
def palindrome?
self.gsub!(/\W+/, '').downcase!
self == self.reverse
end
end
 
module Enumerable
def palindrome?
self.to_a == self.to_a.reverse
end
end
 
p 5.dollar.in(:rupees)
p 5.rupees.in(:yen)
p 5.rupees.in(:yen).between?(7.2, 7.4)
 
p "A man, a plan, a canal -- Panama".palindrome?
p [1,2,3,2,1].palindrome?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.