Решение на Втора задача - хешове, масиви и структура от Николина Гюрова

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

Към профила на Николина Гюрова

Резултати

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

Код

class Hash
def fetch_deep(path)
prop = path.split('.')
result = self
prop.each { |x| result = result[x.to_i] || result[x] || result[x.to_sym] }
result
end
def reshape(shape)
shape.map do |key, value|
[key, value.is_a?(String) ? self.fetch_deep(value) : self.reshape(value)]
end.to_h
end
end
class Array
def reshape(shape)
self.map { |e| e.reshape(shape) }
end
end

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

....F..........

Failures:

  1) 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 `[]' for nil:NilClass
     # /tmp/d20161024-13689-1cqfje2/solution.rb:6:in `block in fetch_deep'
     # /tmp/d20161024-13689-1cqfje2/solution.rb:6:in `each'
     # /tmp/d20161024-13689-1cqfje2/solution.rb:6:in `fetch_deep'
     # /tmp/d20161024-13689-1cqfje2/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)>'

Finished in 0.00802 seconds
15 examples, 1 failure

Failed examples:

rspec /tmp/d20161024-13689-1cqfje2/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys

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

Николина обнови решението на 22.10.2016 21:54 (преди над 7 години)

+class Hash
+ def fetch_deep(path)
+ prop = path.split('.')
+ result = self
+
+ prop.each { |x| result = result[x.to_i] || result[x] || result[x.to_sym] }
+ result
+ end
+
+ def reshape(shape)
+ shape.map do |key, value|
+ [key, value.is_a?(String) ? self.fetch_deep(value) : self.reshape(value)]
+ end.to_h
+ end
+end
+
+class Array
+ def reshape(shape)
+ self.map { |e| e.reshape(shape) }
+ end
+end