How do I lookup a hash?

I have this code and I am trying to pass the tests to look up the
value of the hash to see if its there. How do I do it?

class FoodGroup
  attr_reader :food

  def initialize(food)
    @food = food
  end

  food_groups = {
      "grain" => ['Rice', 'Trigo', 'Avena', 'Barley', 'Flour'],
      "vegetable" => ['Carrot', 'corn' 'Corn', 'Pumpkin', 'Papa'],
      "fruit" => ['Apple', 'Mango', 'Strawberry', 'Peaches', 'Pineapple'],
      "meat" => ['Res', 'Chicken', 'Salmon', 'Fish', 'Pig'],
      "dairy" => ['Milk', 'Yogurt', 'Cheese', 'Cream']
  }

  def lookup()
    p food
  end

end

food_group = FoodGroup.new()

# Driver code
p food_group('Cream') == "dairy"
p food_group('Beef') == "meat"
p food_group('Pineapple') == "fruit"
p food_group('Cane') == "food not found"

The key to solving problems like this is to first forget about the
code, and only think about the logic involved.

You can imagine your hash as a stack of index cards
(each food group one card). What would you need to do
to find the card with the word "Cream" on it?

One way to do it: go through all the index cards,
one by one ("iterate"!), and check whether the word "Cream"
is on the current index card. If yes, return the corresponding
food group and you are done!

To implement this, browse through the docs for the Hash class
(if necessary) and pick the appropriate methods.

Regards,
Marcus

···

Am 13.08.2016 um 01:04 schrieb Joseph Chambers:

I have this code and I am trying to pass the tests to look up the value
of the hash to see if its there. How do I do it?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hello,

I have this code and I am trying to pass the tests to look up the value of the hash to see if its there. How do I do it?

class FoodGroup
  attr_reader :food

  def initialize(food)
    @food = food
  end

  food_groups = {
      "grain" => ['Rice', 'Trigo', 'Avena', 'Barley', 'Flour'],
      "vegetable" => ['Carrot', 'corn' 'Corn', 'Pumpkin', 'Papa'],
      "fruit" => ['Apple', 'Mango', 'Strawberry', 'Peaches', 'Pineapple'],
      "meat" => ['Res', 'Chicken', 'Salmon', 'Fish', 'Pig'],
      "dairy" => ['Milk', 'Yogurt', 'Cheese', 'Cream']
  }

  def lookup()
    p food
  end

end

food_group = FoodGroup.new()

# Driver code
p food_group('Cream') == "dairy"
p food_group('Beef') == "meat"
p food_group('Pineapple') == "fruit"
p food_group('Cane') == "food not found”

Hashes are key-value data structures. So you need a key to fetch the value that you need. Then you need to look into that value for the keyword. There’s a method called “.include?” that you can use to check for a value in an array.

Here is an example:

$ irb
2.2.3 :001 > food_groups = {
2.2.3 :002 > "grain" => ['Rice', 'Trigo', 'Avena', 'Barley', 'Flour'],
2.2.3 :003 > "vegetable" => ['Carrot', 'corn' 'Corn', 'Pumpkin', 'Papa'],
2.2.3 :004 > "fruit" => ['Apple', 'Mango', 'Strawberry', 'Peaches', 'Pineapple'],
2.2.3 :005 > "meat" => ['Res', 'Chicken', 'Salmon', 'Fish', 'Pig'],
2.2.3 :006 > "dairy" => ['Milk', 'Yogurt', 'Cheese', 'Cream']
2.2.3 :007?> }
=> {"grain"=>["Rice", "Trigo", "Avena", "Barley", "Flour"], "vegetable"=>["Carrot", "cornCorn", "Pumpkin", "Papa"], "fruit"=>["Apple", "Mango", "Strawberry", "Peaches", "Pineapple"], "meat"=>["Res", "Chicken", "Salmon", "Fish", "Pig"], "dairy"=>["Milk", "Yogurt", "Cheese", "Cream”]}

2.2.3 :008 > food_groups["grain"].include? 'Rice'
=> true
2.2.3 :009 > food_groups["grain"].include? 'Spaghetti'
=> false
2.2.3 :010 >

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Panagiotis (atmosx) Atmatzidis

email: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

"Everyone thinks of changing the world, but no one thinks of changing himself.” - Leo Tolstoy

···

On 13 Aug 2016, at 02:04, Joseph Chambers <joseph@michael-chambers.com> wrote:

Marcus, Your absolutely correct. I over think the code without thinking
about the step by by step logic for the 'next step'. I will continue
practicing.

···

On Aug 13, 2016 2:36 AM, <sto.mar@web.de> wrote:

Am 13.08.2016 um 01:04 schrieb Joseph Chambers:
> I have this code and I am trying to pass the tests to look up the value
> of the hash to see if its there. How do I do it?

The key to solving problems like this is to first forget about the
code, and only think about the logic involved.

You can imagine your hash as a stack of index cards
(each food group one card). What would you need to do
to find the card with the word "Cream" on it?

One way to do it: go through all the index cards,
one by one ("iterate"!), and check whether the word "Cream"
is on the current index card. If yes, return the corresponding
food group and you are done!

To implement this, browse through the docs for the Hash class
(if necessary) and pick the appropriate methods.

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;