Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Trivia 3

Erik Berlin
December 03, 2015

Ruby Trivia 3

Presented at the Berlin Ruby User Group (RUG::B) on November 5, 2015.

Erik Berlin

December 03, 2015
Tweet

More Decks by Erik Berlin

Other Decks in Programming

Transcript

  1. Ruby
    Trivia
    3

    View Slide

  2. What is the value of the
    global variable $_?
    Question 1:

    View Slide

  3. The String last read by gets.
    Answer 1:

    View Slide

  4. How can you list all

    global variables?
    Bonus Question:

    View Slide

  5. How can you list all

    global variables?
    Bonus Question:
    Answer: Use the Kernel#global_variables method.

    View Slide

  6. How many global variables
    does Ruby define?
    global_variables.count
    Bonus Question:

    View Slide

  7. How many global variables
    does Ruby define?
    global_variables.count
    Bonus Question:
    Answer: 54.

    View Slide

  8. What does Ruby’s -n switch do?
    Question 2:

    View Slide

  9. Causes Ruby to assume the following loop
    around your script, which makes it iterate over
    file name arguments like sed -n or awk.
    while gets
    ...
    end
    Answer 2:

    View Slide

  10. What does Ruby’s -p switch do?
    Bonus Question:

    View Slide

  11. Acts like the -n switch, but prints the
    value of variable $_ at the each end of
    the loop. For example:
    ruby -p -e '$_.tr! "a-z", "A-Z"' < file
    Bonus Question:

    View Slide

  12. What thread-local variable can
    only store four possible values?
    Question 3:

    View Slide

  13. What thread-local variable can
    only store four possible values?
    Question 3:
    Hint #1: Those values are 0, 1, 2, and 3.

    View Slide

  14. What thread-local variable can
    only store four possible values?
    Question 3:
    Hint #1: Those values are 0, 1, 2, and 3.
    Hint #2: The value is 0 by default and can only increase.

    View Slide

  15. $SAFE
    Answer 3:
    Trick question because it looks like a global variable,
    even though it behaves like a thread-local variable.

    View Slide

  16. $SAFE
    Answer 3:
    There used to be $SAFE = 4 but it was removed in Ruby 2.1.
    Supposedly, it was only ever used by one company in Japan.

    View Slide

  17. How can you check if
    an object is trusted?
    Bonus Question:

    View Slide

  18. How can you check if
    an object is trusted?
    Bonus Question:
    Answer: Use the Kernel#tainted? method.

    View Slide

  19. What happens if you do this?
    module Kernel
    def tainted?
    return false
    end
    end
    Question 4:

    View Slide

  20. The tainted? method will
    always return false, but Ruby
    will still track tainted state via
    an internal FL_TAINT flag.
    Answer 4:

    View Slide

  21. How can you mark a

    tainted object as safe?
    Question 5:

    View Slide

  22. Kernel#untaint
    a.k.a. Kernel#trust
    $SAFE = 1
    foo = gets.trust
    eval(foo)
    Answer 5:

    View Slide

  23. Thanks for playing!
    Follow @sferik on Twitter for
    more Ruby trivia and practica.

    View Slide