Решение на Втора задача - хешове, масиви и структура от Сияна Плачкова

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

Към профила на Сияна Плачкова

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 5 успешни тест(а)
  • 10 неуспешни тест(а)

Код

class Hash
def fetch_deep(path)
access = self
path.split('.').each do |key|
key = key == key.to_i.to_s ? key.to_i : key.intern
is_val_hash = access.is_a?(Hash) && access.key?(key)
is_val_arr = access.is_a?(Array) && key < access.size
access = is_val_hash || is_val_arr ? access[key] : nil
end
access
end
end

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

F....F.FFFFFFFF

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(dessert.fetch_deep('variant')).to eq('chocolate')
       
       expected: "chocolate"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-11p5akp/spec.rb:25: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)>'

  2) Task 2 Hash#fetch_deep is indifferent to symbols and strings
     Failure/Error: expect(input.fetch_deep('dessert')).to eq 'ice cream'
       
       expected: "ice cream"
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-11p5akp/spec.rb:96: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)>'

  3) Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
     Failure/Error: expect(input.fetch_deep('nested.1')).to eq :a
       
       expected: :a
            got: nil
       
       (compared using ==)
     # /tmp/d20161024-13689-11p5akp/spec.rb:111: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)>'

  4) Task 2 Hash#reshape can rename fields
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `reshape' for {:name=>"Georgi"}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:122: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)>'

  5) Task 2 Hash#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `reshape' for {:profile=>{:name=>"Georgi"}}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:133: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)>'

  6) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `reshape' for {:name=>"Georgi"}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:146: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)>'

  7) Task 2 Hash#reshape assigns nil to unknown keys
     Failure/Error: expect(input.reshape(shape)).to eq b: nil
     NoMethodError:
       undefined method `reshape' for {:a=>1}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:153: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)>'

  8) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `reshape' for {:users=>[{:name=>"Georgi"}, {:name=>"Ivan"}]}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:177: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)>'

  9) Task 2 Hash#reshape does not modify the input hash
     Failure/Error: input.reshape(shape)
     NoMethodError:
       undefined method `reshape' for {:menu=>{:order=>"cake", "dessert"=>"ice cream", 3=>4}}:Hash
     # /tmp/d20161024-13689-11p5akp/spec.rb:194: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)>'

  10) Task 2 Array#reshape maps each element with the result of Hash#fetch_deep
     Failure/Error: expect(input.reshape(shape)).to eq [
     NoMethodError:
       undefined method `reshape' for [{:order=>{:item=>"meal"}}, {:order=>{:item=>"dessert"}}]:Array
     # /tmp/d20161024-13689-11p5akp/spec.rb:213: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.00786 seconds
15 examples, 10 failures

Failed examples:

rspec /tmp/d20161024-13689-11p5akp/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-11p5akp/spec.rb:92 # Task 2 Hash#fetch_deep is indifferent to symbols and strings
rspec /tmp/d20161024-13689-11p5akp/spec.rb:108 # Task 2 Hash#fetch_deep can fetch integer-like keys from hashes
rspec /tmp/d20161024-13689-11p5akp/spec.rb:117 # Task 2 Hash#reshape can rename fields
rspec /tmp/d20161024-13689-11p5akp/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-11p5akp/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-11p5akp/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys
rspec /tmp/d20161024-13689-11p5akp/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-11p5akp/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash
rspec /tmp/d20161024-13689-11p5akp/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Сияна обнови решението на 24.10.2016 16:51 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ access = self
+ path.split('.').each do |key|
+ key = key == key.to_i.to_s ? key.to_i : key.intern
+ is_val_hash = access.is_a?(Hash) && access.key?(key)
+ is_val_arr = access.is_a?(Array) && key < access.size
+ access = is_val_hash || is_val_arr ? access[key] : nil
+ end
+ access
+ end
+end