News for hackers. Stuff that matters.
Re:Ruby gaining a safe navigation operator: '.?' by MatmaRex on Monday October 26, 2015 @07:16AM (#10450639)
PHP does this (with a warning). I'm not sure if this should be an argument for or against.

    <?php
    $foo = array();
    echo $foo['a']['b']['c']; // not an error
Re:Ruby gaining a safe navigation operator: '.?' by juriansluiman on Monday October 26, 2015 @08:02AM (#10450791)
That's where the coalesce operator ("??") is for. Exactly to suppress the warning and avoid verbose isset() calls:

    echo $foo['a']['b'] ?? 'default_value';
Re:Ruby gaining a safe navigation operator: '.?' by Udo on Monday October 26, 2015 @08:32AM (#10450921)
I'm looking forward to this in PHP7. My current workaround, which exists for the same reason as the coalesce operator (https://wiki.php.net/rfc/isset_ternary) is a custom function:

  $v = @first($var1, $var2, 'some-default');
which will simply return the first parameter that has a non-null, non-empty-string value.
Re:Ruby gaining a safe navigation operator: '.?' by vidarh on Monday October 26, 2015 @07:37AM (#10450717)
Here's a slightly evil way of handling this specific case in Ruby:

   d = lambda {|hash,key| Hash.new(&d) }
   foo = Hash.new(&d)
   p foo['a']['b']['c']
The argument to "Hash.new" is the default value, or a on object that takes the hash object itself and the key, and returns the default value (and optionally stores it in the hash, which makes it great for memoization). In this case, whenever accessing a non-existent hash key, it returns another hash with the same closure as the default...

Of course it's likely to blow up in all kinds of ways since anything trying to do "if foo['a']" to check of 'a' is present in foo will break, for example.