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

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

Към профила на Милен Дончев

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 7 успешни тест(а)
  • 8 неуспешни тест(а)

Код

def get_next_element(element, index)
key = (element.is_a?Array) ? index.to_i : index.to_sym
current = element[key]
current = element[key.to_s] if current.nil? && (!key.is_a?Integer)
current
end
class Hash
@next_deep_element
def fetch_deep_recursive(current_element, components)
if (current_element.is_a?Hash) || (current_element.is_a?Array)
@next_deep_element = current_element
fetch_deep components[1..-1].join('.')
else
@next_deep_element = nil
current_element
end
end
def fetch_deep(path)
components = path.split('.')
@next_deep_element ||= self
current = get_next_element(@next_deep_element, components.first)
fetch_deep_recursive(current, components)
end
end

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

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

  3) 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-6gfohx/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 `reshape' for {:name=>"Georgi"}:Hash
     # /tmp/d20161024-13689-6gfohx/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 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-6gfohx/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)>'

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

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

  8) 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-6gfohx/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.00788 seconds
15 examples, 8 failures

Failed examples:

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

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

Милен обнови решението на 24.10.2016 08:57 (преди над 7 години)

+def get_next_element(element, index)
+ key = (element.is_a?Array) ? index.to_i : index.to_sym
+
+ current = element[key]
+ current = element[key.to_s] if current.nil? && (!key.is_a?Integer)
+ current
+end
+
+class Hash
+ @next_deep_element
+
+ def fetch_deep_recursive(current_element, components)
+ if (current_element.is_a?Hash) || (current_element.is_a?Array)
+ @next_deep_element = current_element
+ fetch_deep components[1..-1].join('.')
+ else
+ @next_deep_element = nil
+ current_element
+ end
+ end
+
+ def fetch_deep(path)
+ components = path.split('.')
+ @next_deep_element ||= self
+ current = get_next_element(@next_deep_element, components.first)
+ fetch_deep_recursive(current, components)
+ end
+end