This file contains 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
require 'autotest/growl' | |
Autotest.add_hook :initialize do |autotest| | |
%w{.git .svn .hg .DS_Store db log tmp vendor ._* .sqlite3}.each do |exception| | |
autotest.add_exception(exception) | |
end | |
end |
This file contains 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
module Calculator | |
def self.calculate(expression) | |
raise ArgumentError, 'Must be a string expression' unless expression.is_a? String | |
raise ArgumentError, 'Must be numeric arithmetic' unless expression =~ pattern | |
sets = lambda { expression.scan(pattern) } | |
sets.call.drop(1). | |
reduce(evaluate(*sets.call.first)) do |last_result, equation| | |
evaluate(last_result, *equation[1..2]) |
This file contains 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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | |
describe "Calculator" do | |
it "should blow up if a non-string expression is supplied" do | |
lambda { Calculator::calculate(2) }.should raise_error(ArgumentError) | |
end | |
it "should blow up if an invalid expression is supplied" do | |
lambda { Calculator::calculate("a + b") }.should raise_error(ArgumentError) | |
end |
This file contains 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
module Calculator | |
def self.calculate(expression) | |
raise ArgumentError, 'Must be a string expression' unless expression.is_a? String | |
raise ArgumentError, 'Must be numeric arithmetic' unless expression =~ /\d\s[\+\-\*\/]\s\d/ | |
parts = [] | |
def parts.combine(value) | |
replace([value.to_s]) | |
end | |
expression.split(/\s/).each do |val| |
This file contains 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
module Calculator | |
def self.calculate(expression) | |
raise ArgumentError, 'Must be a string expression' unless expression.is_a? String | |
raise ArgumentError, 'Must be numeric arithmetic' unless expression =~ /\d\s[\+\-\*\/]\s\d/ | |
parts = [] | |
expression.split(/\s/).each do |val| | |
parts << val | |
if parts.size == 3 | |
result = parts[0].to_i.send(parts[1].to_sym, parts[2].to_i) |
This file contains 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
module Calculator | |
def self.calculate(expression) | |
raise ArgumentError, 'Must be a string expression' unless expression.is_a? String | |
raise ArgumentError, 'Must be numeric arithmetic' unless expression =~ pattern | |
parts = [] | |
expression.scan(pattern) do |operand, operator| | |
parts[0] = parts.empty? ? operand : parts[0].to_i.send(parts[1], operand.to_i) | |
parts[1] = operator | |
end |
This file contains 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
module Calculator | |
def self.calculate(expression) | |
raise ArgumentError, 'Must be a string expression' unless expression.is_a? String | |
raise ArgumentError, 'Must be numeric arithmetic' unless expression =~ pattern | |
value = nil | |
expression.scan(pattern) do |left, operator, right| | |
value ||= left | |
value = eval(value + operator + right).to_s | |
end |
This file contains 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
a, b = 'hey', 'now' | |
v1 = <<Foo.upcase | |
#{a + b} | |
Foo | |
puts v1 | |
v2 = <<'Foo'.upcase | |
#{a + b} | |
Foo |
This file contains 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
require 'stub' | |
class Foo | |
def bar(hey, now) | |
end | |
def hoy() | |
end | |
end |
This file contains 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 Stub | |
def initialize(clazz, include_super = false) | |
@clazz = clazz | |
meta = class << self; self; end | |
methods = clazz.public_instance_methods(include_super) | |
@called_table = Hash.new(false) | |
@returns_table = Hash.new() | |
@errors_table = Hash.new() | |
methods.each do |method_name| | |
add_reader(meta, "#{method_name}_called?") { |