# Deleting from hash question

**URL:** <https://www.ruby-forum.com/t/deleting-from-hash-question/55147>\
**Category:** Rails\
**Created:** [March 17, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147 "2006-03-17T12:11:28Z")\
**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:11pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147/1 "2006-03-17T12:11:28Z")

</div>

I’m a ruby newbie and have what is probably a relatively simple problem  
I’m trying to solve. 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, 5:21pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147/2 "2006-03-17T17:21:05Z")

</div>

Sorry, perhaps I just didn’t provide enough specific info. Trying again:

Since this is a multi-user system, there is no way to query the database  
for unassigned tasks. I’m only inserting lines in the assignments table  
when they currently exist for a particular person. If a “task” is not  
assigned, then there is no entry for that task and person combo. So, all  
I really have to go by is the static hash list of TASKS, which is a  
complete list of all available tasks in the system which could  
potentially be assigned to a person. And I need to compare the hash from  
the database of “currently assigned” tasks to my static one of “all  
available” so that I can remove them and be left with a list of  
“currently unassigned tasks”.

I want to have a method in the assignments model with  
self.find\_unassigned(person), but since I can’t query the result, I need  
to figure out how to compare my hash against the already existing  
self.find\_assignments() method. It’s the comparing of the two (very  
different) hashes that is perplexing me. I have this:

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

and I have something like this that rail’s “find” gives me on the  
assignments table:

db\_result = {  
“123” =\> {“id” =\> “123”, “person\_id” =\> “11”, “task” =\> “homework”},  
“456” =\> {“id” =\> “456”, “person\_id” =\> “11”, “task” =\> “chores”},  
“789” =\> {“id” =\> “789”, “person\_id” =\> “11”, “task” =\> “exercise”},  
}

So, using some elegant ruby code that I can’t seem to find, I need to  
dig down into the db result hash, looping through and comparing it to my  
static hash, and then remove the key=\>value pairs from my static hash in  
the instances where has\_value? matches.

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, 5:24pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147/3 "2006-03-17T17:24:59Z")

</div>

On Mar 17, 2006, at 3:11 AM, Ryan W. wrote:

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

Here this should be what you want:

ezra-zygmuntowiczs-computer:~/sub ez$ irb  
irb(main):001:0\> TASKS = {  
irb(main):002:1\* “homework” =\> “Do Your Homework”,  
irb(main):003:1\* “chores” =\> “Your Have Chores”,  
irb(main):004:1\* “exercise” =\> “Don’t Forget to Exercise”  
irb(main):005:1\> }  
=\> {“chores”=\>“Your Have Chores”, “homework”=\>“Do Your Homework”,  
“exercise”=\>“Don’t Forget to Exercise”}  
irb(main):006:0\> TASKS.delete “chores”  
=\> “Your Have Chores”  
irb(main):007:0\> TASKS  
=\> {“homework”=\>“Do Your Homework”, “exercise”=\>“Don’t Forget to  
Exercise”}

Cheers-  
-Ezra

---

<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, 6:11pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147/4 "2006-03-17T18:11:49Z")

</div>

On 3/17/06, Ryan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> available" so that I can remove them and be left with a list of  
> “homework” =\> “Do Your Homework”,  
> “456” =\> {“id” =\> “456”, “person\_id” =\> “11”, “task” =\> “chores”},  
> “789” =\> {“id” =\> “789”, “person\_id” =\> “11”, “task” =\> “exercise”},  
> }
> 
> So, using some elegant ruby code that I can’t seem to find, I need to  
> dig down into the db result hash, looping through and comparing it to my  
> static hash, and then remove the key=\>value pairs from my static hash in  
> the instances where has\_value? matches.
> 
> Suggestions?

Haven’t tested, but maybe something like this?

db\_result.each {|row| TASKS.delete(row[‘task’])}

Justin

---

<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, 10:55pm UTC](https://www.ruby-forum.com/t/deleting-from-hash-question/55147/5 "2006-03-17T22:55:01Z")

</div>

> Haven’t tested, but maybe something like this?
> 
> db\_result.each {|row| TASKS.delete(row[‘task’])}

That didn’t quite work, but thanks! The answer ended up being to creat  
the following method in my Attachment model:

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

where “assigned” is my @current\_assignments hash passed by the  
controller.

(credit to James Edward G. II for the answer)
