Note that there are some explanatory texts on larger screens.

plurals
  1. POHowto fake a hash index?
    primarykey
    data
    text
    <p>Program I'm making has a simple configuration file looking something like this.</p> <pre><code>@overlays = { :foo =&gt; "http://www.bar.com", :bar =&gt; nil, } </code></pre> <p>What I need to do is go through this hash and get the following output.</p> <pre><code>OverlayKey[0]='foo' OverlayVal[0]='http://www.bar.com' OverlayKey[1]='bar' OverlayVal[1]='nil' </code></pre> <p>In order to keep my configuration like I want it I need some fake index numbers. Would rather not add numbers into the hash, it would make the configuration look a bit ugly. So I been playing around with artificially generating the numbers during output.</p> <p>This is ugly but I"m just playing around with it currently.</p> <pre><code>def makenumbers @numbers = [] length = @overlays.length - 1 (0..length).each do |num| @numbers.push("#{num}") end end makenumbers @overlays.each do |key,val| @numbers.each do |num| puts "OverlayKey['#{num}']='#{key}'" puts "OverlayVal['#{num}']='#{val}'" end end </code></pre> <p>Which is giving me something like this.</p> <pre><code>OverlayKey['0']='foo' OverlayVal['0']='http://www.bar.com' OverlayKey['1']='foo' OverlayVal['1']='http://www.bar.com' OverlayKey['0']='bar' OverlayVal['0']='' OverlayKey['1']='bar' OverlayVal['1']='' </code></pre> <p>Understand why this doesn't give me the output I want, although after playing with it for a bit I'm not really sure how to do what I want without adding numbers into the hash during configuration. Sure this is pretty simple I just can't seem to wrap my head around it.</p>
    singulars
    1. This table or related slice is empty.
    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. 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