Решение на Втора задача - хешове, масиви и структура от Добромира Лозева

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

Към профила на Добромира Лозева

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 9 успешни тест(а)
  • 6 неуспешни тест(а)

Код

class Hash
def fetch_deep(path, hash = self)
return hash if hash.nil? || path.nil?
key, path = path.split(".", 2)
return fetch_deep(path, hash[key]) if hash.key?(key)
return fetch_deep(path, hash[key.to_sym]) if hash.key?(key.to_sym)
end
def reshape(shape)
my_new_hash = {}
my_new_hash.merge!(shape)
my_new_hash.each { |key, value| my_new_hash[key] = fetch_deep(value) }
my_new_hash
end
end

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

F..F.....FF.F.F

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.fetch_deep('dessert.comments.0.text')).to eq('So sweet!')
     NoMethodError:
       undefined method `key?' for #<Array:0x007fdb16614308>
     # /tmp/d20161024-13689-16fljcy/solution.rb:5:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/spec.rb:17: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 can find values in arrays by index
     Failure/Error: expect(input.fetch_deep('orders.0.meal')).to eq 'cake'
     NoMethodError:
       undefined method `key?' for [{:meal=>"cake"}, {:meal=>"ice cream"}]:Array
     # /tmp/d20161024-13689-16fljcy/solution.rb:5:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/spec.rb:81: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#reshape can extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"profile.name"}:Hash
     # /tmp/d20161024-13689-16fljcy/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `block in reshape'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `each'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `reshape'
     # /tmp/d20161024-13689-16fljcy/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)>'

  4) Task 2 Hash#reshape can create nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"name"}:Hash
     # /tmp/d20161024-13689-16fljcy/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `block in reshape'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `each'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `reshape'
     # /tmp/d20161024-13689-16fljcy/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)>'

  5) Task 2 Hash#reshape can extract fields from arrays by index
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `split' for {:first_name=>"users.0.name", :second_name=>"users.1.name"}:Hash
     # /tmp/d20161024-13689-16fljcy/solution.rb:4:in `fetch_deep'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `block in reshape'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `each'
     # /tmp/d20161024-13689-16fljcy/solution.rb:12:in `reshape'
     # /tmp/d20161024-13689-16fljcy/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)>'

  6) 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-16fljcy/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.0078 seconds
15 examples, 6 failures

Failed examples:

rspec /tmp/d20161024-13689-16fljcy/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-16fljcy/spec.rb:73 # Task 2 Hash#fetch_deep can find values in arrays by index
rspec /tmp/d20161024-13689-16fljcy/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-16fljcy/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-16fljcy/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-16fljcy/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Добромира обнови решението на 24.10.2016 16:51 (преди около 8 години)

+class Hash
+ def fetch_deep(path, hash = self)
+ return hash if hash.nil? || path.nil?
+ key, path = path.split(".", 2)
+ return fetch_deep(path, hash[key]) if hash.key?(key)
+ return fetch_deep(path, hash[key.to_sym]) if hash.key?(key.to_sym)
+ end
+
+ def reshape(shape)
+ my_new_hash = {}
+ my_new_hash.merge!(shape)
+ my_new_hash.each { |key, value| my_new_hash[key] = fetch_deep(value) }
+ my_new_hash
+ end
+end