Craig Russell wrote on 1/25/18 8:48 PM:
> 
>> On Jan 25, 2018, at 8:44 AM, Sam Ruby <[email protected]> wrote:
...snip...
>> Looks to me like the redirect worked, but the if check did not do what
>> you expected.  @phase is neither 'discuss' nor 'vote'.  Just a guess,
>> but perhaps what you want is @progress['phase'] as this data was read
>> from JSON so the hash indexes are likely to be strings.
> 
> This was totally unexpected. I thought Ruby was supposed to be user friendly. 
> :)

Debug printouts and stackoverflow are your friend - even when you've
been writing code for a while.  8-)

> @progress['phase'] is different from @progress[:phase]

Yes, also depending on *where* @progress[] came from - i.e. is it a hash
you built in memory, or a hash you read from some JSON/YAML file?  I
just got bit by this one too recently.

Symbols (a name with a colon in :front or sometimes after:) are not
Strings, as much as they sometimes look like them.

- Hashes created in memory (or passed around a program) using :symbols
will work great (and are fast).

- Hashes with symbols for keys written out to disk are converted into
strings.  When you read the Hash back from disk, it's now *string* keys.

- Symbols also have a different way to write: when defining a {} value:

ShaneMac:wdev curcuru$ irb
irb(main):001:0> h = {'string_key' => 'value', :symbol_key => 'other
value', symbol_special: 'short form' }
=> {"string_key"=>"value", :symbol_key=>"other value",
:symbol_special=>"short form"}

irb(main):002:0> h[:symbol_special]
=> "short form"

irb(main):003:0> h[:new_symbol] = 'extra value'
=> "extra value"

irb(main):004:0> h
=> {"string_key"=>"value", :symbol_key=>"other value",
:symbol_special=>"short form", :new_symbol=>"extra value"}

But - if you read the hash in from disk, all the keys will be strings.

Make more sense now?
> 
> I just learnt about :phase the symbol recently, and now I have to learn more? 
> :(
> 
> Anyway, prob solved.
> 
> THANKS!
> 
> Craig
> 
> P.S. Who knew this was so complicated? ;-)


-- 

- Shane
  https://www.apache.org/foundation/marks/resources

Reply via email to