Created
March 17, 2012 21:25
-
-
Save andrewdavidcostello/2065412 to your computer and use it in GitHub Desktop.
SaaS-Class: Homework 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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