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

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

Към профила на Антон Сотиров

Резултати

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

Код

def check_type(hash, x)
if hash.is_a? Array then hash[x.to_i]
elsif hash.key?(x) then hash.fetch(x)
elsif hash.key?(x.to_sym) then hash.fetch(x.to_sym)
else return nil
end
end
class Hash
def fetch_deep(path)
hash = self
path.split(".").each do |x|
hash = check_type(hash, x)
end
hash
end
end
class Hash
def reshape(hash)
reshapred_hash = hash
reshapred_hash.each do |k, v|
reshapred_hash[k] = self.fetch_deep(v) if v.is_a? String
end
end
end

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

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

Failures:

  1) Task 2 README works for all examples
     Failure/Error: expect(order.reshape(shape)).to eq({food: {type: 'cake', taste: 'chocolate'}})
       
       expected: {:food=>{:type=>"cake", :taste=>"chocolate"}}
            got: {:food=>{:type=>"dessert.type", :taste=>"dessert.variant"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:food => {:type=>"cake", :taste=>"chocolate"}
       +:food => {:type=>"dessert.type", :taste=>"dessert.variant"}
     # /tmp/d20161024-13689-51sirs/spec.rb:44: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 `key?' for nil:NilClass
     # /tmp/d20161024-13689-51sirs/solution.rb:3:in `check_type'
     # /tmp/d20161024-13689-51sirs/solution.rb:13:in `block in fetch_deep'
     # /tmp/d20161024-13689-51sirs/solution.rb:12:in `each'
     # /tmp/d20161024-13689-51sirs/solution.rb:12:in `fetch_deep'
     # /tmp/d20161024-13689-51sirs/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 extract fields to nested objects
     Failure/Error: expect(input.reshape(shape)).to eq output
       
       expected: {:me=>{:first_name=>"Georgi"}}
            got: {:me=>{:first_name=>"profile.name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:me => {:first_name=>"Georgi"}
       +:me => {:first_name=>"profile.name"}
     # /tmp/d20161024-13689-51sirs/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
       
       expected: {:profile=>{:first_name=>"Georgi"}}
            got: {:profile=>{:first_name=>"name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:profile => {:first_name=>"Georgi"}
       +:profile => {:first_name=>"name"}
     # /tmp/d20161024-13689-51sirs/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
       
       expected: {:me=>{:first_name=>"Georgi", :second_name=>"Ivan"}}
            got: {:me=>{:first_name=>"users.0.name", :second_name=>"users.1.name"}}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:me => {:first_name=>"Georgi", :second_name=>"Ivan"}
       +:me => {:first_name=>"users.0.name", :second_name=>"users.1.name"}
     # /tmp/d20161024-13689-51sirs/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-51sirs/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.01206 seconds
15 examples, 6 failures

Failed examples:

rspec /tmp/d20161024-13689-51sirs/spec.rb:3 # Task 2 README works for all examples
rspec /tmp/d20161024-13689-51sirs/spec.rb:85 # Task 2 Hash#fetch_deep returns nil for non-existant keys
rspec /tmp/d20161024-13689-51sirs/spec.rb:125 # Task 2 Hash#reshape can extract fields to nested objects
rspec /tmp/d20161024-13689-51sirs/spec.rb:136 # Task 2 Hash#reshape can create nested objects
rspec /tmp/d20161024-13689-51sirs/spec.rb:156 # Task 2 Hash#reshape can extract fields from arrays by index
rspec /tmp/d20161024-13689-51sirs/spec.rb:205 # Task 2 Array#reshape maps each element with the result of Hash#fetch_deep

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

Антон обнови решението на 24.10.2016 03:20 (преди около 8 години)

+def check_type(hash, x)
+ if hash.is_a? Array then hash[x.to_i]
+ elsif hash.key?(x) then hash.fetch(x)
+ elsif hash.key?(x.to_sym) then hash.fetch(x.to_sym)
+ else return nil
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ hash = self
+ path.split(".").each do |x|
+ hash = check_type(hash, x)
+ end
+ hash
+ end
+end
+
+class Hash
+ def reshape(hash)
+ reshapred_hash = hash
+ reshapred_hash.each do |k, v|
+ reshapred_hash[k] = self.fetch_deep(v) if v.is_a? String
+ end
+ end
+end