Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Writing a "delete named product from hash" method</strong></p> <p>There are shorter ways of doing it, but shooting for clarity I came up with this:</p> <pre><code>products = {124 =&gt; ['shoes', 59.99], 352 =&gt; ['shirt', 19.99], 777 =&gt; ['pants', 19.87], 667 =&gt; ['jacket', 39.99], 898 =&gt; ['shoulder_holster', 22.78]} def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end puts products.inspect wipeProduct(products,'pants') puts products.inspect wipeProduct(products,'shoulder_holster') puts products.inspect bash-3.2$ ruby prod.rb {352=&gt;["shirt", 19.99], 898=&gt;["shoulder_holster", 22.78], 667=&gt;["jacket", 39.99], 777=&gt;["pants", 19.87], 124=&gt;["shoes", 59.99]} {352=&gt;["shirt", 19.99], 898=&gt;["shoulder_holster", 22.78], 667=&gt;["jacket", 39.99], 124=&gt;["shoes", 59.99]} {352=&gt;["shirt", 19.99], 667=&gt;["jacket", 39.99], 124=&gt;["shoes", 59.99]} </code></pre> <p>I don't know if it's possible for "pants" to occur in the hash in multiple places, but since I used "hash.each(...)", the method wipeProduct(hash, nameToDelete) will test every hash entry.</p> <p><strong>The input type bug and how to fix it</strong></p> <p>When you take input, you're assigning the string you captured to d. Here's the proof:</p> <pre><code>irb(main):010:0&gt; d = gets.to_s 12 =&gt; "12\n" irb(main):011:0&gt; d.class =&gt; String </code></pre> <p>You can convert that string to a Fixnum like this:</p> <pre><code>irb(main):012:0&gt; d.to_i =&gt; 12 irb(main):013:0&gt; d.to_i.class =&gt; Fixnum </code></pre> <p>All keys in the products hash are Fixnums. Here's the proof:</p> <pre><code>irb(main):014:0&gt; products.keys.each {|i| puts i.class} Fixnum Fixnum Fixnum Fixnum Fixnum =&gt; [352, 898, 667, 777, 124] </code></pre> <p>So you need to capture the value for the argument with this line:</p> <pre><code>d = gets.to_i # Get value for argument </code></pre> <p><strong>The deletion part of the answer:</strong></p> <p>From products, you can delete the pants entry programmatically with this:</p> <p>products.delete(777)</p> <p>Running it gets you this:</p> <pre><code>irb(main):003:0&gt; products.delete(777) =&gt; ["pants", 19.87] </code></pre> <p>Notice that you supply the key value (in this case 777) to .delete() and that it returns an array consisting of the key and value in that order respectively.</p> <p><strong>An alternative implementation</strong></p> <p>I'm not sure if it's safe to modify a hash in a block that's iterating over the key-value pairs in the hash. If it isn't, you can just save up all the keys to be deleted and delete them after iterating over the hash:</p> <pre><code>def wipeProduct(hash, nameToDelete) keysToDelete = [] hash.each do |i| key = i[0] productName = i[1].first keysToDelete &lt;&lt; key if productName==nameToDelete end keysToDelete.each {|key| hash.delete(key) } end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload