Решение на Бонус задача: ArnoldC+- от Георги Иванов

Обратно към всички решения

Към профила на Георги Иванов

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 1 успешни тест(а)
  • 28 неуспешни тест(а)

Код

module ArnoldCPM
def self.totally_recall(&block)
ArnoldC.new(@printer).instance_eval(&block)
end
def self.printer=(printer)
@printer = printer
end
end
module ArithmeticOperations
# + x
def get_up(x)
@var_assignment << "#{@variable_name} += #{x}\n"
@value += x
end
# - x
def get_down(x)
@var_assignment << "#{@variable_name} -= #{x}\n"
@value -= x
end
# * x
def youre_fired(x)
@var_assignment << "#{@variable_name} *= #{x}\n"
@value *= x
end
# / x
def he_had_to_split(x)
@var_assignment << "#{@variable_name} /= #{x}\n"
@value /= x
end
# % x
def i_let_him_go(x)
@var_assignment << "#{@variable_name} %= #{x}\n"
@value %= x
end
end
module LogicalOperations
# i_lied = 0
# no_problemo = 1
# or x
def consider_that_a_divorce(x)
@var_assignment << if @value != 0
"#{x} "
else
"#{@value} "
end
end
# and x
def knock_knock(x)
@var_assignment << if @value == 0
"#{@value} "
else
"#{x} "
end
end
end
module Comparison
# > x
def let_off_some_steam_bennet(x)
return 1 if @value > x
0
end
# == x
def you_are_not_you_you_are_me(x)
return 1 if @value == x
0
end
end
module IfElseStatements
def because_im_going_to_say_please(condition)
@cond_statement << "if (#{condition != 0})\n"
end
def bull_shit
@cond_statement << "else\n"
end
# do_something_else
def you_have_no_respect_for_logic
@cond_statement << "end\n"
@code << @cond_statement
end
end
module VariableAssignment
include ArithmeticOperations
include LogicalOperations
def variables
@variables ||= {}
end
def get_to_the_chopper(name)
@variable_name = name
end
def here_is_my_invitation(initial_value)
@value = initial_value
@var_assignment = "#{@variable_name} = #{initial_value}\n"
end
def enough_talk
variables[@variable_name] = @value
@code << @var_assignment
end
def method_missing(method_name)
variables[method_name] || method_name
end
end
module Function
def functions
@functions ||= {}
end
def listen_to_me_very_carefully(function_name)
@lt_one_argument = true
@is_void = true
@function_name = function_name
@code = "def #{function_name}"
end
def i_need_your_clothes_your_boots_and_your_motorcycle(x)
@code << "," unless @lt_one_argument
@lt_one_argument = false
@code << " #{x}"
end
def give_these_people_air
@is_void = false
end
def ill_be_back(return_value)
@code << "return #{return_value || 0}"
end
def hasta_la_vista_baby
functions[@function_name] = @is_void
@code << "end"
end
def do_it_now(name, *args)
@code << if functions[name]
"#{name}()\n"
else
"#{name}(#{args.join(", ")})"
end
end
end
class ArnoldC
include VariableAssignment
include Comparison
include Function
def initialize(printer)
@printer = printer
end
def its_showtime
@code = "def main\n"
end
def you_have_been_terminated
@code << "end\n"
self.instance_eval @code
end
def talk_to_the_hand(some_var)
@code << "#{@printer}.print #{some_var}\n"
end
end

Лог от изпълнението

.FFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ArnoldCPM has true and false constants
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(0)
           expected: 1 time with arguments: (0)
           received: 0 times with arguments: (0)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) ArnoldCPM can assign variables
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(42)
           expected: 1 time with arguments: (42)
           received: 0 times with arguments: (42)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) ArnoldCPM can define and call functions with arguments
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(11)
           expected: 1 time with arguments: (11)
           received: 0 times with arguments: (11)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) ArnoldCPM can define and call functions with return value
     Failure/Error: get_your_ass_to_mars _invocation_result
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:508:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:499:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  5) ArnoldCPM returns 0 by default
     Failure/Error: ill_be_back
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:143:in `ill_be_back'
     # /tmp/d20170103-15620-iipm1l/spec.rb:528:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:525:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  6) ArnoldCPM can take functions as arguments
     Failure/Error: because_im_going_to_say_please _should_invoke_it
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:571:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:556:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  7) ArnoldCPM can return functions
     Failure/Error: get_your_ass_to_mars _forty_two_printer
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:605:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:593:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  8) ArnoldCPM defines new inner functions for each function invocation
     Failure/Error: get_your_ass_to_mars _first_invocation
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:635:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:625:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  9) ArnoldCPM supports basic recursion
     Failure/Error: get_up 1
     NoMethodError:
       undefined method `+' for :_from:Symbol
     # /tmp/d20170103-15620-iipm1l/solution.rb:15:in `get_up'
     # /tmp/d20170103-15620-iipm1l/spec.rb:674:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:666:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  10) ArnoldCPM can calculate fibonacci(20) recursively
     Failure/Error: let_off_some_steam_bennet _n
     ArgumentError:
       comparison of Fixnum with :_n failed
     # /tmp/d20170103-15620-iipm1l/solution.rb:68:in `>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:68:in `let_off_some_steam_bennet'
     # /tmp/d20170103-15620-iipm1l/spec.rb:720:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:714:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  11) ArnoldCPM can use closures in very convoluted ways
     Failure/Error: get_your_ass_to_mars _result
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:776:in `block (3 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:765:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  12) ArnoldCPM has algebra that can sum numbers
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(8)
           expected: 1 time with arguments: (8)
           received: 0 times with arguments: (8)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  13) ArnoldCPM has algebra that can subtract numbers
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(3)
           expected: 1 time with arguments: (3)
           received: 0 times with arguments: (3)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  14) ArnoldCPM has algebra that can multiply numbers
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(10)
           expected: 1 time with arguments: (10)
           received: 0 times with arguments: (10)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  15) ArnoldCPM has algebra that can divide numbers
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(2)
           expected: 1 time with arguments: (2)
           received: 0 times with arguments: (2)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  16) ArnoldCPM has algebra that can calculate modulo
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(2)
           expected: 1 time with arguments: (2)
           received: 0 times with arguments: (2)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  17) ArnoldCPM has algebra that can chain multiple operations
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(42)
           expected: 1 time with arguments: (42)
           received: 0 times with arguments: (42)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  18) ArnoldCPM has algebra that can use variables in calculations
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(21)
           expected: 1 time with arguments: (21)
           received: 0 times with arguments: (21)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  19) ArnoldCPM has boolean arithmetic that given *or* returns truthy values if one of the operands is truthy
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(1)
           expected: 1 time with arguments: (1)
           received: 0 times with arguments: (1)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  20) ArnoldCPM has boolean arithmetic that given *or* returns falsy values if both operands are falsy
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(0)
           expected: 1 time with arguments: (0)
           received: 0 times with arguments: (0)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  21) ArnoldCPM has boolean arithmetic that given *or* between two truthy values returns the first one
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(11)
           expected: 1 time with arguments: (11)
           received: 0 times with arguments: (11)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  22) ArnoldCPM has boolean arithmetic that given *and* returns falsy values if either operand is falsy
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(0)
           expected: 1 time with arguments: (0)
           received: 0 times with arguments: (0)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  23) ArnoldCPM has boolean arithmetic that given *and* between two truthy values returns the second one
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(22)
           expected: 1 time with arguments: (22)
           received: 0 times with arguments: (22)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  24) ArnoldCPM has boolean arithmetic that has the same precedence of *or* and *and* operations
     Failure/Error: expect(printer).to receive(:print).with(value_to_be_printed).ordered
       (Double "printer").print(0)
           expected: 1 time with arguments: (0)
           received: 0 times with arguments: (0)
     # /tmp/d20170103-15620-iipm1l/spec.rb:878:in `block in expect_execution'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  25) ArnoldCPM has branching mechanism that can execute if statements
     Failure/Error: because_im_going_to_say_please no_problemo
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:336:in `block (4 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:334:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  26) ArnoldCPM has branching mechanism that can execute if-else statements
     Failure/Error: because_im_going_to_say_please no_problemo
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:363:in `block (4 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:361:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  27) ArnoldCPM has branching mechanism that can nest if-else statements
     Failure/Error: because_im_going_to_say_please no_problemo
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:410:in `block (4 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:408:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  28) ArnoldCPM has branching mechanism that considers functions truthy
     Failure/Error: because_im_going_to_say_please _function
     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # /tmp/d20170103-15620-iipm1l/solution.rb:116:in `method_missing'
     # /tmp/d20170103-15620-iipm1l/spec.rb:456:in `block (4 levels) in <top (required)>'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `instance_eval'
     # /tmp/d20170103-15620-iipm1l/solution.rb:3:in `totally_recall'
     # /tmp/d20170103-15620-iipm1l/spec.rb:451:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.03083 seconds
29 examples, 28 failures

Failed examples:

rspec /tmp/d20170103-15620-iipm1l/spec.rb:15 # ArnoldCPM has true and false constants
rspec /tmp/d20170103-15620-iipm1l/spec.rb:28 # ArnoldCPM can assign variables
rspec /tmp/d20170103-15620-iipm1l/spec.rb:466 # ArnoldCPM can define and call functions with arguments
rspec /tmp/d20170103-15620-iipm1l/spec.rb:489 # ArnoldCPM can define and call functions with return value
rspec /tmp/d20170103-15620-iipm1l/spec.rb:517 # ArnoldCPM returns 0 by default
rspec /tmp/d20170103-15620-iipm1l/spec.rb:540 # ArnoldCPM can take functions as arguments
rspec /tmp/d20170103-15620-iipm1l/spec.rb:583 # ArnoldCPM can return functions
rspec /tmp/d20170103-15620-iipm1l/spec.rb:613 # ArnoldCPM defines new inner functions for each function invocation
rspec /tmp/d20170103-15620-iipm1l/spec.rb:651 # ArnoldCPM supports basic recursion
rspec /tmp/d20170103-15620-iipm1l/spec.rb:695 # ArnoldCPM can calculate fibonacci(20) recursively
rspec /tmp/d20170103-15620-iipm1l/spec.rb:760 # ArnoldCPM can use closures in very convoluted ways
rspec /tmp/d20170103-15620-iipm1l/spec.rb:45 # ArnoldCPM has algebra that can sum numbers
rspec /tmp/d20170103-15620-iipm1l/spec.rb:62 # ArnoldCPM has algebra that can subtract numbers
rspec /tmp/d20170103-15620-iipm1l/spec.rb:79 # ArnoldCPM has algebra that can multiply numbers
rspec /tmp/d20170103-15620-iipm1l/spec.rb:96 # ArnoldCPM has algebra that can divide numbers
rspec /tmp/d20170103-15620-iipm1l/spec.rb:113 # ArnoldCPM has algebra that can calculate modulo
rspec /tmp/d20170103-15620-iipm1l/spec.rb:130 # ArnoldCPM has algebra that can chain multiple operations
rspec /tmp/d20170103-15620-iipm1l/spec.rb:154 # ArnoldCPM has algebra that can use variables in calculations
rspec /tmp/d20170103-15620-iipm1l/spec.rb:183 # ArnoldCPM has boolean arithmetic that given *or* returns truthy values if one of the operands is truthy
rspec /tmp/d20170103-15620-iipm1l/spec.rb:218 # ArnoldCPM has boolean arithmetic that given *or* returns falsy values if both operands are falsy
rspec /tmp/d20170103-15620-iipm1l/spec.rb:235 # ArnoldCPM has boolean arithmetic that given *or* between two truthy values returns the first one
rspec /tmp/d20170103-15620-iipm1l/spec.rb:252 # ArnoldCPM has boolean arithmetic that given *and* returns falsy values if either operand is falsy
rspec /tmp/d20170103-15620-iipm1l/spec.rb:287 # ArnoldCPM has boolean arithmetic that given *and* between two truthy values returns the second one
rspec /tmp/d20170103-15620-iipm1l/spec.rb:304 # ArnoldCPM has boolean arithmetic that has the same precedence of *or* and *and* operations
rspec /tmp/d20170103-15620-iipm1l/spec.rb:324 # ArnoldCPM has branching mechanism that can execute if statements
rspec /tmp/d20170103-15620-iipm1l/spec.rb:347 # ArnoldCPM has branching mechanism that can execute if-else statements
rspec /tmp/d20170103-15620-iipm1l/spec.rb:378 # ArnoldCPM has branching mechanism that can nest if-else statements
rspec /tmp/d20170103-15620-iipm1l/spec.rb:441 # ArnoldCPM has branching mechanism that considers functions truthy

История (1 версия и 1 коментар)

Георги обнови решението на 29.12.2016 18:21 (преди над 7 години)

+module ArnoldCPM
+ def self.totally_recall(&block)
+ ArnoldC.new(@printer).instance_eval(&block)
+ end
+
+ def self.printer=(printer)
+ @printer = printer
+ end
+end
+
+module ArithmeticOperations
+ # + x
+ def get_up(x)
+ @var_assignment << "#{@variable_name} += #{x}\n"
+ @value += x
+ end
+
+ # - x
+ def get_down(x)
+ @var_assignment << "#{@variable_name} -= #{x}\n"
+ @value -= x
+ end
+
+ # * x
+ def youre_fired(x)
+ @var_assignment << "#{@variable_name} *= #{x}\n"
+ @value *= x
+ end
+
+ # / x
+ def he_had_to_split(x)
+ @var_assignment << "#{@variable_name} /= #{x}\n"
+ @value /= x
+ end
+
+ # % x
+ def i_let_him_go(x)
+ @var_assignment << "#{@variable_name} %= #{x}\n"
+ @value %= x
+ end
+end
+
+module LogicalOperations
+ # i_lied = 0
+ # no_problemo = 1
+
+ # or x
+ def consider_that_a_divorce(x)
+ @var_assignment << if @value != 0
+ "#{x} "
+ else
+ "#{@value} "
+ end
+ end
+
+ # and x
+ def knock_knock(x)
+ @var_assignment << if @value == 0
+ "#{@value} "
+ else
+ "#{x} "
+ end
+ end
+end
+module Comparison
+ # > x
+ def let_off_some_steam_bennet(x)
+ return 1 if @value > x
+ 0
+ end
+
+ # == x
+ def you_are_not_you_you_are_me(x)
+ return 1 if @value == x
+ 0
+ end
+end
+module IfElseStatements
+ def because_im_going_to_say_please(condition)
+ @cond_statement << "if (#{condition != 0})\n"
+ end
+
+ def bull_shit
+ @cond_statement << "else\n"
+ end
+
+ # do_something_else
+ def you_have_no_respect_for_logic
+ @cond_statement << "end\n"
+ @code << @cond_statement
+ end
+end
+
+module VariableAssignment
+ include ArithmeticOperations
+ include LogicalOperations
+
+ def variables
+ @variables ||= {}
+ end
+
+ def get_to_the_chopper(name)
+ @variable_name = name
+ end
+
+ def here_is_my_invitation(initial_value)
+ @value = initial_value
+ @var_assignment = "#{@variable_name} = #{initial_value}\n"
+ end
+
+ def enough_talk
+ variables[@variable_name] = @value
+ @code << @var_assignment
+ end
+
+ def method_missing(method_name)
+ variables[method_name] || method_name
+ end
+end
+
+module Function
+ def functions
+ @functions ||= {}
+ end
+
+ def listen_to_me_very_carefully(function_name)
+ @lt_one_argument = true
+ @is_void = true
+ @function_name = function_name
+ @code = "def #{function_name}"
+ end
+
+ def i_need_your_clothes_your_boots_and_your_motorcycle(x)
+ @code << "," unless @lt_one_argument
+ @lt_one_argument = false
+ @code << " #{x}"
+ end
+
+ def give_these_people_air
+ @is_void = false
+ end
+
+ def ill_be_back(return_value)
+ @code << "return #{return_value || 0}"
+ end
+
+ def hasta_la_vista_baby
+ functions[@function_name] = @is_void
+ @code << "end"
+ end
+
+ def do_it_now(name, *args)
+ @code << if functions[name]
+ "#{name}()\n"
+ else
+ "#{name}(#{args.join(", ")})"
+ end
+ end
+end
+
+class ArnoldC
+ include VariableAssignment
+ include Comparison
+ include Function
+
+ def initialize(printer)
+ @printer = printer
+ end
+
+ def its_showtime
+ @code = "def main\n"
+ end
+
+ def you_have_been_terminated
+ @code << "end\n"
+ self.instance_eval @code
+ end
+
+ def talk_to_the_hand(some_var)
+ @code << "#{@printer}.print #{some_var}\n"
+ end
+end

Като цяло стратегията за отделяне на логика "събери кофа подобни методи в модул и го include-ни" единствено води до това да търсиш имплементацията на едно нещо на много места.

Както вероятно се досещаш, не ни допада много идеята за "акумулурай един голям низ и го eval-ни". Eval is evil. Още повече ако дефинира методи по този начин. Какво става ако искам да си дефинирам ArnoldC+- функция с име initialize?


Може да погледнеш нашето решение ако ти е интересно как сме подходили към проблема. Надявам се задачата да ти е била забавна и полезна.