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

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

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

Резултати

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

Код

class Hash
def fetch_deep(input)
hash = {}
self.each do |key, value|
hash[key] = value
end.to_h
new_hash = hash.values.to_s.gsub(/[\[\]\"\\]/, '')
return nil if new_hash.include?(input)
new_hash
end
end

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

F.FFFF.FFFFFFFF

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.fetch_deep('dessert.variant')).to eq('chocolate')
       
       expected: "chocolate"
            got: "{:type=>cake, :variant=>chocolate, :rating=>10, :comments=>{:text=>So sweet!}, {:text=>A perfect blend of milk chocolate and cookies. With a cherry on top.}}"
       
       (compared using ==)
     # /tmp/d20161024-13689-1k8fes3/spec.rb:16: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 look up deeply nested values
     Failure/Error: expect(input.fetch_deep('order.meal.type')).to eq 'dessert'
       
       expected: "dessert"
            got: "{:meal=>{:type=>dessert}}"
       
       (compared using ==)
     # /tmp/d20161024-13689-1k8fes3/spec.rb:70: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 find values in arrays by index
     Failure/Error: expect(input.fetch_deep('orders.0.meal')).to eq 'cake'
       
       expected: "cake"
            got: "{:meal=>cake}, {:meal=>ice cream}"
       
       (compared using ==)
     # /tmp/d20161024-13689-1k8fes3/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)>'

  4) Task 2 Hash#fetch_deep returns nil for non-existant keys
     Failure/Error: expect(input.fetch_deep('meal')).to be nil
       
       expected #<NilClass:8> => nil
            got #<String:70001329914320> => ""
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       `expect(actual).to eq(expected)` if you don't care about
       object identity in this example.
     # /tmp/d20161024-13689-1k8fes3/spec.rb:88: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#fetch_deep is indifferent to symbols and strings
     Failure/Error: expect(input.fetch_deep('order')).to eq 'cake'
       
       expected: "cake"
            got: "cake, ice cream"
       
       (compared using ==)
     # /tmp/d20161024-13689-1k8fes3/spec.rb:95: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#fetch_deep can fetch integer-like keys from hashes
     Failure/Error: expect(input.fetch_deep('nested.1')).to eq :a
       
       expected: :a
            got: "{1=>:a, :2=>:b}"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:a
       +"{1=>:a, :2=>:b}"
     # /tmp/d20161024-13689-1k8fes3/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)>'

  7) 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-1k8fes3/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)>'

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

  9) 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-1k8fes3/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)>'

  10) 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-1k8fes3/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)>'

  11) 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-1k8fes3/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)>'

  12) 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-1k8fes3/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)>'

  13) 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-1k8fes3/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.00883 seconds
15 examples, 13 failures

Failed examples:

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

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

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

+class Hash
+ def fetch_deep(input)
+ hash = {}
+ self.each do |key, value|
+ hash[key] = value
+ end.to_h
+ new_hash = hash.values.to_s.gsub(/[\[\]\"\\]/, '')
+ return nil if new_hash.include?(input)
+ new_hash
+ end
+end