Skip to content

@andrewdavidcostello /cartesian.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
SaaS-Class: Homework 2
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 }
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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.