Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove a key from Hash and get the remaining hash in Ruby/Rails?

#11
It's was great if delete return the delete pair of the hash.
I'm doing this:

hash = {a: 1, b: 2, c: 3}
{b: hash.delete(:b)} # => {:b=>2}
hash # => {:a=>1, :c=>3}
Reply

#12
Multiple ways to delete Key in Hash.
you can use any Method from below

hash = {a: 1, b: 2, c: 3}
hash.except!(:a) # Will remove *a* and return HASH
hash # Output :- {b: 2, c: 3}

hash = {a: 1, b: 2, c: 3}
hash.delete(:a) # will remove *a* and return 1 if *a* not present than return nil

So many ways is there, you can look on Ruby doc of Hash [here](

[To see links please register here]

).

Thank you

Reply

#13
There are many ways to remove a key from a hash and get the remaining hash in Ruby.

1. `.slice` => It will return selected keys and not delete them from the original hash. Use `slice!` if you want to remove the keys permanently else use simple `slice`.

2.2.2 :074 > hash = {"one"=>1, "two"=>2, "three"=>3}
=> {"one"=>1, "two"=>2, "three"=>3}
2.2.2 :075 > hash.slice("one","two")
=> {"one"=>1, "two"=>2}
2.2.2 :076 > hash
=> {"one"=>1, "two"=>2, "three"=>3}

2. `.delete` => It will delete the selected keys from the original hash(it can accept only one key and not more than one).

2.2.2 :094 > hash = {"one"=>1, "two"=>2, "three"=>3}
=> {"one"=>1, "two"=>2, "three"=>3}
2.2.2 :095 > hash.delete("one")
=> 1
2.2.2 :096 > hash
=> {"two"=>2, "three"=>3}

3. `.except` => It will return the remaining keys but not delete anything from the original hash. Use `except!` if you want to remove the keys permanently else use simple `except`.

2.2.2 :097 > hash = {"one"=>1, "two"=>2, "three"=>3}
=> {"one"=>1, "two"=>2, "three"=>3}
2.2.2 :098 > hash.except("one","two")
=> {"three"=>3}
2.2.2 :099 > hash
=> {"one"=>1, "two"=>2, "three"=>3}


4. `.delete_if` => In case you need to remove a key based on a value. It will obviously remove the matching keys from the original hash.

2.2.2 :115 > hash = {"one"=>1, "two"=>2, "three"=>3, "one_again"=>1}
=> {"one"=>1, "two"=>2, "three"=>3, "one_again"=>1}
2.2.2 :116 > value = 1
=> 1
2.2.2 :117 > hash.delete_if { |k,v| v == value }
=> {"two"=>2, "three"=>3}
2.2.2 :118 > hash
=> {"two"=>2, "three"=>3}

5. `.compact` => It is used to remove all `nil` values from the hash. Use `compact!` if you want to remove the `nil` values permanently else use simple `compact`.

2.2.2 :119 > hash = {"one"=>1, "two"=>2, "three"=>3, "nothing"=>nil, "no_value"=>nil}
=> {"one"=>1, "two"=>2, "three"=>3, "nothing"=>nil, "no_value"=>nil}
2.2.2 :120 > hash.compact
=> {"one"=>1, "two"=>2, "three"=>3}

Results based on Ruby 2.2.2.
Reply

#14
Try the `except!` method.

{:a => 1, :b => 2}.except!(:a) #=> {:b => 2}

Reply

#15
Why not just use:

hash.delete(key)

`hash` is now the "remaining hash" you're looking for.
Reply

#16
## Hash#except (Ruby 3.0+)

Starting from Ruby 3.0, [Hash#except](

[To see links please register here]

) is a build-in method.

As a result, there is no more need to depend on ActiveSupport or write monkey-patches in order to use it.

```ruby
h = { a: 1, b: 2, c: 3 }
p h.except(:a) #=> {:b=>2, :c=>3}
```

**Sources:**

- [Hash#except](

[To see links please register here]

) from official Ruby docs.
- [Link to the PR](

[To see links please register here]

).
- [Ruby 3.0 adds Hash#except and ENV.except](

[To see links please register here]

).

Reply

#17
For those of you who just came here to know how to delete a key/value pair from a hash, you can use:<br>
`hash.delete(key)`

For the rest of you who came here to read a wall of text about something entirely different, you can read the rest of this answer:

[Rails has an except/except! method][1] that returns the hash with those keys removed. If you're already using Rails, there's no sense in creating your own version of this.

class Hash
# Returns a hash that includes everything but the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except(:c) # => { a: true, b: false}
# hash # => { a: true, b: false, c: nil}
#
# This is useful for limiting a set of parameters to everything but a few known toggles:
# @person.update(params[:person].except(:admin))
def except(*keys)
dup.except!(*keys)
end

# Replaces the hash without the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except!(:c) # => { a: true, b: false}
# hash # => { a: true, b: false }
def except!(*keys)
keys.each { |key| delete(key) }
self
end
end

[1]:

[To see links please register here]

Reply

#18
Use `delete`, `except`, or `except!`

`sample_hash = {hey: 'hey', hello: 'hello'}`

Delete:
```
sample_hash.delete(:hey)
=> 'hey'

sample_hash
=> {hello: 'hello'}
```
Returns value of the key and deletes the key in the original object, returns nil if no such key

Except:
```
sample_hash.except(:hey)
=> {hello: 'hello'}

sample_hash
=> {hey: 'hey', hello: 'hello'}
```
Returns the entire hash without the specified keys, but does not update the original hash

Except!:
`except!` is the same as except but it permanently changes the state of the original hash like all bang operated methods do

```
sample_hash.except!(:hey)
=> {hello: 'hello'}

sample_hash
=> {hello: 'hello'}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through