# Deleting from hash question

**URL:** <https://www.ruby-forum.com/t/deleting-from-hash-question/83114>\
**Category:** Ruby\
**Created:** [March 17, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114 "2006-03-17T12:32:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 17, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114/1 "2006-03-17T12:32:34Z")

</div>

I’m a ruby newbie and have what is probably a relatively simple problem  
I’m trying to solve (though I’m using rails, this is more a ruby  
question). Say that I created a hash:

TASKS = {  
“homework” =\> “Do Your Homework”,  
“chores” =\> “Your Have Chores”,  
“exercise” =\> “Don’t Forget to Exercise”  
}

and then I had an Assignment model with:

def self.find\_assignments(person)  
find(:all, :conditions =\> [“person\_id = ?”, person .id])  
end

Which I called in the controller like so:

@current\_assignments = Assignment.find\_assignments(@person)

So, @current\_assignments works fine. Now, the problem. I want to display  
the tasks which have NOT yet been assigned (so that they can be assigned  
if desired). In the assignments table, I have a column called ‘tasks’  
which correlates with the keys in the TASKS hash. So I just want to  
remove any key=\>value pairs from TASKS that match with the ‘tasks’  
column in @current\_assignment and then loop through the resulting new  
hash in the view.

I’m at a loss as how best to do this. It must be something so simple  
it’s eluding me. Any suggestions?

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 17, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114/2 "2006-03-17T15:28:11Z")

</div>

On Mar 17, 2006, at 5:32 AM, Ryan W. wrote:

> and then I had an Assignment model with:
> 
> def self.find\_assignments(person)  
> find(:all, :conditions =\> [“person\_id = ?”, person .id])

Just FYI, Rails’s dynamic finder methods are probably a little  
cleaner way to do the above:

find\_all\_by\_person\_in(person.id)

> if desired). In the assignments table, I have a column called ‘tasks’  
> which correlates with the keys in the TASKS hash. So I just want to  
> remove any key=\>value pairs from TASKS that match with the ‘tasks’  
> column in @current\_assignment and then loop through the resulting new  
> hash in the view.
> 
> I’m at a loss as how best to do this. It must be something so simple  
> it’s eluding me. Any suggestions?

How about something like:

todo = Hash[ \*TASKS.reject do |name, desc|  
@current\_assignments.any? { |assign| assign.tasks == name }  
end.flatten ]

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 17, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114/3 "2006-03-17T20:03:24Z")

</div>

> Just FYI, Rails’s dynamic finder methods are probably a little  
> cleaner way to do the above:
> 
> find\_all\_by\_person\_in(person.id)

Yep. I totally flaked on that. Thanks!

> How about something like:
> 
> todo = Hash[ \*TASKS.reject do |name, desc|  
> @current\_assignments.any? { |assign| assign.tasks == name }  
> end.flatten ]

Okay… this seems to work fine right up to the .flatten  
I created a method in my model like so:

def self.get\_unassigned(assigned)  
Hash[ \*TASKS.reject do |name, desc|  
assigned.any? { |assign| assign.task == name }  
end.flatten ]  
end

with “assigned” being @currently\_assigned.

Rails is giving me:

NoMethodError in Admin#show

undefined method `flatten’ for {“homework”=\>“Do Your Homework!”}:Hash

At least it’s correctly eleminating the already assigned tasks. Any idea  
why I’m getting thrown this exception on a standard ruby method? It seem  
to happen to me a lot.

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 17, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114/4 "2006-03-17T20:17:29Z")

</div>

On Mar 17, 2006, at 1:03 PM, Ryan W. wrote:

> ```
> Hash[ *TASKS.reject do |name, desc|
> 
> ```
> 
> undefined method `flatten’ for {“homework”=\>“Do Your Homework!”}:Hash  
> This is me being dumb and not realizing how clever Ruby is. You can  
> drop the flatten() and the Hash[] call:

TASKS.reject do |name, desc|  
assigned.any? { |assign| assign.task == name }  
end

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [March 17, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/83114/5 "2006-03-17T20:42:03Z")

</div>

James G. wrote:

> You can  
> drop the flatten() and the Hash[] call:
> 
> TASKS.reject do |name, desc|  
> assigned.any? { |assign| assign.task == name }  
> end

That did it, thanks! You’ve been a HUGE help!
