- Коректно
- 1 успешни тест(а)
- 0 неуспешни тест(а)
. Finished in 0.00397 seconds 1 example, 0 failures
Срокът за предаване на решения е отминал
Понеже ви казахме, че Monkey patching-а е нещо, което трябва да се избягва по принцип, решихме отново да ви накараме да го приложите. :-)
Дефинирайте метод Object#thread
, който приема списък от произволен брой
операции, които имат вида на Proc
, lambda
, или Symbol
, и ги прилага
последователно отляво надясно, по следния начин:
Подава като аргумент на първата операция обекта, на който е извикан thread
,
резултатът от това прилагане се подава като аргумент на втората операция и така
нататък, докато не се изчерпят всички операции. Ако операцията е подадена като
Symbol
, то резултатът не се подава като аргумент, а му се вика съответния
метод (вижте примерите).
Пример:
-42.thread :abs, -> x { x ** 3 }, :succ, -> x { x - 2 } # Резултат: 74087
Горният пример е еквивалентен на израза:
((((-42.abs) ** 3).succ) - 2)
Пример:
"Marvin".thread :size, -> x { x ** 4 }, :to_s, -> s { s.split "" }, -> a { a[0] + a[-1] }, :to_i # Резултат: 16
Горният пример е еквивалентен на израза:
(((-> a { a[0] + a[1] }).(((("Marvin".size) ** 4).to_s).split "")).to_i)
За още примери разгледайте sample_spec-a.
P.S. Има вероятност да ви е полезен методът Object#send
, който приема като
аргумент символ и извиква метода на обекта, който има същото име като символа,
например:
-42.send :abs # Извиква -42.abs и връща 42
Както винаги, ваш приятел е документацията
. Finished in 0.00397 seconds 1 example, 0 failures
. Finished in 0.01661 seconds 1 example, 0 failures
. Finished in 0.02518 seconds 1 example, 0 failures
. Finished in 0.09608 seconds 1 example, 0 failures
. Finished in 0.01671 seconds 1 example, 0 failures
. Finished in 0.0269 seconds 1 example, 0 failures
. Finished in 0.04049 seconds 1 example, 0 failures
. Finished in 0.01994 seconds 1 example, 0 failures
. Finished in 0.05092 seconds 1 example, 0 failures
. Finished in 0.02791 seconds 1 example, 0 failures
. Finished in 0.02137 seconds 1 example, 0 failures
. Finished in 0.00281 seconds 1 example, 0 failures
. Finished in 0.02112 seconds 1 example, 0 failures
. Finished in 0.0463 seconds 1 example, 0 failures
. Finished in 0.03104 seconds 1 example, 0 failures
. Finished in 0.02003 seconds 1 example, 0 failures
. Finished in 0.01767 seconds 1 example, 0 failures
. Finished in 0.02001 seconds 1 example, 0 failures
. Finished in 0.00238 seconds 1 example, 0 failures
. Finished in 0.02118 seconds 1 example, 0 failures
. Finished in 0.02496 seconds 1 example, 0 failures
. Finished in 0.00301 seconds 1 example, 0 failures
. Finished in 0.00298 seconds 1 example, 0 failures
. Finished in 0.00249 seconds 1 example, 0 failures
. Finished in 0.00288 seconds 1 example, 0 failures
. Finished in 0.00244 seconds 1 example, 0 failures
. Finished in 0.02493 seconds 1 example, 0 failures
. Finished in 0.0024 seconds 1 example, 0 failures
. Finished in 0.00257 seconds 1 example, 0 failures
. Finished in 0.00273 seconds 1 example, 0 failures
. Finished in 0.02293 seconds 1 example, 0 failures
. Finished in 0.01988 seconds 1 example, 0 failures
. Finished in 0.00283 seconds 1 example, 0 failures
. Finished in 0.00239 seconds 1 example, 0 failures
. Finished in 0.04645 seconds 1 example, 0 failures
. Finished in 0.00293 seconds 1 example, 0 failures
. Finished in 0.00246 seconds 1 example, 0 failures
. Finished in 0.00288 seconds 1 example, 0 failures
. Finished in 0.00236 seconds 1 example, 0 failures
F Failures: 1) Object.thread works with multiple operations Failure/Error: 42.thread(->(x) { x / 6 }, :succ, ->(x) { [1] * x }, :size).should eq 8 NoMethodError: undefined method `call' for :succ:Symbol # /tmp/d20131106-4393-13wphle/solution.rb:4:in `block in thread' # /tmp/d20131106-4393-13wphle/solution.rb:3:in `each' # /tmp/d20131106-4393-13wphle/solution.rb:3:in `reduce' # /tmp/d20131106-4393-13wphle/solution.rb:3:in `thread' # /tmp/d20131106-4393-13wphle/spec.rb:3:in `block (2 levels) in <top (required)>' # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>' # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>' Finished in 0.00148 seconds 1 example, 1 failure Failed examples: rspec /tmp/d20131106-4393-13wphle/spec.rb:2 # Object.thread works with multiple operations
. Finished in 0.0228 seconds 1 example, 0 failures
. Finished in 0.06718 seconds 1 example, 0 failures
. Finished in 0.06789 seconds 1 example, 0 failures
. Finished in 0.05269 seconds 1 example, 0 failures
. Finished in 0.04151 seconds 1 example, 0 failures
. Finished in 0.01908 seconds 1 example, 0 failures
. Finished in 0.02883 seconds 1 example, 0 failures
. Finished in 0.0168 seconds 1 example, 0 failures
. Finished in 0.01605 seconds 1 example, 0 failures
. Finished in 0.0177 seconds 1 example, 0 failures
. Finished in 0.02273 seconds 1 example, 0 failures
. Finished in 0.02273 seconds 1 example, 0 failures
. Finished in 0.01989 seconds 1 example, 0 failures
. Finished in 0.02184 seconds 1 example, 0 failures
. Finished in 0.02358 seconds 1 example, 0 failures
. Finished in 0.02289 seconds 1 example, 0 failures
. Finished in 0.05507 seconds 1 example, 0 failures
. Finished in 0.05485 seconds 1 example, 0 failures
F Failures: 1) Object.thread works with multiple operations Failure/Error: 42.thread(->(x) { x / 6 }, :succ, ->(x) { [1] * x }, :size).should eq 8 NoMethodError: private method `thread' called for 42:Fixnum # /tmp/d20131106-4393-1s7ixyt/spec.rb:3:in `block (2 levels) in <top (required)>' # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>' # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>' Finished in 0.01939 seconds 1 example, 1 failure Failed examples: rspec /tmp/d20131106-4393-1s7ixyt/spec.rb:2 # Object.thread works with multiple operations