2

I'm trying to delete an attribute and its value from a hash. Its seems simple based on answers I see on here but it doesn't appear to work for me. Curious if anyone has any thoughts as to why? Also... this is NOT a duplicate of the question that was linked. I have tried except and slice... neither of those work as well. I'm guessing my dataset it different.

Here is an example hash I have:

 {:data=>[{:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :color=>"silver", :vin=>"123456789F22"},{=>[{:id=>2, :make=>"Mazda", :model=>"RX7", :year=>1980, :color=>"blue", :vin=>"123456789F22"},{=>[{:id=>3, :make=>"Chevy", :model=>"Dorado", :year=>2018, :color=>"white", :vin=>"123456789F22"}]}

I have tried the following:

 hashval.delete("color")
 hashval.except!("color")
 hashval.each {|h| h.delete("color")}  

I also tried :color in case the string format was wrong

 hashval.delete(:color)
 hashval.except!(:color)
 hashval.each {|h| h.delete(:color)}  

but when I try to display the resulting hash

 logger.info "hash result: #{hashval}"

I still see the original hash with the color still in there. Any thoughts on what I am doing wrong?

Ok... more info! If I do this:

 hashval.delete(:data)

It does delete :data (and everything else after that). So it has something to do with the attributes in that hash array?

As it turns out, the answer is:

 hashval = { data: vehicles.map { |v| v.table_data.except(:color) } }

I guess this issue was marked closed as a duplicate (even though it wasn't) so I cant add the solution.

Blake
  • 375
  • 2
  • 6
  • 19
  • http://rubyquicktips.com/post/603292403/accessing-a-hash-with-either-string-or-symbol-keys https://apidock.com/rails/Hash/symbolize_keys – Brad Werth Mar 14 '18 at 14:51
  • 3
    It's so straightforward. `delete` method is perfect. As @simone, @Kris comments, It should work for you. – Mr. Black Mar 14 '18 at 15:06
  • Don't use the reserved keywords. – Mr. Black Mar 14 '18 at 15:07
  • is `data` a key? or do you mean to say `hashval = [{:id => 1, .... }]`? – Kris Mar 14 '18 at 16:40
  • When I print out the hash that's what it shows... it prefixes data in there. I didn't write the code but it comes from this: hashval= { data:vehicles.map(&:table_data) } – Blake Mar 14 '18 at 16:45
  • @MrYoshiji are you going to review this to see it is not a duplicate? The data is very different and I have tried everything in that link. I'm guessing my issue is the data but don't know how to get around it. – Blake Mar 15 '18 at 17:39
  • `hashval = { data: vehicles.map { |v| v.table_data.except(:color) } }` should fix your problem – MrYoshiji Mar 15 '18 at 17:50
  • @MrYoshiji that was it! very nice solution... thanks!!!! – Blake Mar 15 '18 at 18:52

2 Answers2

3

You keys are symbols so, hash.delete(:color) should work:

h = {:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :color=>"silver", :vin=>"123456789F22"}

h.key?(:color) # => true

h.delete(:color)

h.key?(:color) # => false

h # => {:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :vin=>"123456789F22"}

Also hash might be a reserved word since if I open irb or console and type hash I get back an integer. I have no idea what it is, but it makes me think hash shouldn't be used as a var.

Kris
  • 19,188
  • 9
  • 91
  • 111
  • 1
    FYI `hash` is a method: http://ruby-doc.org/core-2.4.0/Object.html#method-i-hash – Stefan Mar 14 '18 at 15:00
  • thanks... I have renamed it to hashval so that's not an issue. See my comment below to Simones answer... I updated my problem with actual dataset. – Blake Mar 14 '18 at 16:31
  • also: https://stackoverflow.com/questions/2505824/how-hash-string-method-work – Kris Mar 14 '18 at 16:38
0

hash#delete works if you use a symbol:

irb
irb(main):001:0> hash = {:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :color=>"silver", :vin=>"123456789F22"}
=> {:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :color=>"silver", :vin=>"123456789F22"}
irb(main):002:0> hash.delete(:color)
=> "silver"
irb(main):003:0> hash
=> {:id=>1, :make=>"Ford", :model=>"Excursion", :year=>2018, :vin=>"123456789F22"}
Simone
  • 20,302
  • 14
  • 79
  • 103
  • thanks... when I try my earlier example in IRB it works! I have since edited my problem to put the larger dataset. I cant paste that into IRB without getting errors. Maybe that's the problem... since the hash is multiple sets? – Blake Mar 14 '18 at 16:29
  • You have pretty much asked a new question now. – Kris Mar 14 '18 at 16:41
  • 1
    Sorry... I thought I was making the question easier by only putting a subset of the hash... turns out I was wrong! – Blake Mar 14 '18 at 16:50