User Tools

Site Tools


daily:2021-06-16

2021-06-15 Entry

Heap

IIf - Immediate If is a function used in visual basic that give a similar functionality to a ternary operator, however it has the fault of side effects where each expression will be ran and the return is decided by the testing expression.

Ruby `and` operator - low presedence can be made use of in a one line if statement to both set a value and break or next a loop. e.g.

def check_pegs(guesses, secrets)
    hints = Array.new(4)
    guesses.each_with_index do | g, i |
        hints[i] = 'B' and next if g == secrets[i] 
        secrets.each_with_index do | s, j |
            hints[j] = 'W' and break if g == s && hints[j].nil?
        end
    end
    peg_count(hints)
end

However this can be confusing as a standard && operator would set the value then next/break only if the statement after the if was

More explanation of the and vs &&

Ruby precedence list

Cull Coalescing Operators in Ruby

Wiki - Null Coalescing Operators

Wiki - Memoization

daily/2021-06-16.txt · Last modified: 2021/06/16 21:13 by chrisbolas