Решение на Втора задача - хешове, масиви и структура от Александър Илиев

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

Към профила на Александър Илиев

Резултати

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

Код

class Hash
def fetch_deep(path)
splited_path = path.split(/\./, 2)
key = key?(splited_path[0]) ? splited_path[0] : splited_path[0].to_sym
return fetch(key, nil) if splited_path.length == 1
self[key].fetch_deep(splited_path[1])
end
end
class Array
def fetch_deep(path)
splited_path = path.split(/\./, 2)
return self[splited_path[0].to_i] if splited_path.length == 1
self[splited_path[0].to_i].fetch_deep(splited_path[1])
end
end

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

F...F...FFFFFFF

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.reshape(shape)).to eq({food: 'cake', taste: 'chocolate'})
     NoMethodError:
       undefined method `reshape' for {:dessert=>{:type=>"cake", :variant=>"chocolate"}}:Hash
     # /tmp/d20161024-13689-rbpdce/spec.rb:35: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 returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal.0.type')).to be nil
     NoMethodError:
       undefined method `fetch_deep' for nil:NilClass
     # /tmp/d20161024-13689-rbpdce/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-rbpdce/spec.rb:89: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 rename fields
     Failure/Error: expect(input.reshape(shape)).to eq output
     NoMethodError:
       undefined method `reshape' for {:name=>"Georgi"}:Hash
     # /tmp/d20161024-13689-rbpdce/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)>'

  4) 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-rbpdce/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)>'

  5) 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-rbpdce/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)>'

  6) 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-rbpdce/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)>'

  7) 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-rbpdce/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)>'

  8) 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-rbpdce/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)>'

  9) 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-rbpdce/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.00787 seconds
15 examples, 9 failures

Failed examples:

rspec /tmp/d20161024-13689-rbpdce/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-rbpdce/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-rbpdce/spec.rb:117 # Task 2 Hash#reshape can rename fields
rspec /tmp/d20161024-13689-rbpdce/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-rbpdce/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-rbpdce/spec.rb:149 # Task 2 Hash#reshape assigns nil to unknown keys
rspec /tmp/d20161024-13689-rbpdce/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-rbpdce/spec.rb:180 # Task 2 Hash#reshape does not modify the input hash
rspec /tmp/d20161024-13689-rbpdce/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Александър обнови решението на 23.10.2016 16:09 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ splited_path = path.split(/\./, 2)
+ key = key?(splited_path[0]) ? splited_path[0] : splited_path[0].to_sym
+ return fetch(key, nil) if splited_path.length == 1
+ self[key].fetch_deep(splited_path[1])
+ end
+end
+
+class Array
+ def fetch_deep(path)
+ splited_path = path.split(/\./, 2)
+ return self[splited_path[0].to_i] if splited_path.length == 1
+ self[splited_path[0].to_i].fetch_deep(splited_path[1])
+ end
+end