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.

8
  • rubyquicktips.com/post/603292403/… apidock.com/rails/Hash/symbolize_keys Commented Mar 14, 2018 at 14:51
  • 3
    It's so straightforward. delete method is perfect. As @simone, @Kris comments, It should work for you. Commented Mar 14, 2018 at 15:06
  • Don't use the reserved keywords. Commented Mar 14, 2018 at 15:07
  • is data a key? or do you mean to say hashval = [{:id => 1, .... }]? Commented Mar 14, 2018 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) } Commented Mar 14, 2018 at 16:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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"}

3 Comments

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?
You have pretty much asked a new question now.
Sorry... I thought I was making the question easier by only putting a subset of the hash... turns out I was wrong!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.