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

Ruby Trivia 3

1f74b13f1e5c6c69cb5d7fbaabb1e2cb?s=47 Erik Berlin
December 03, 2015

Ruby Trivia 3

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

1f74b13f1e5c6c69cb5d7fbaabb1e2cb?s=128

Erik Berlin

December 03, 2015
Tweet

Transcript

  1. Ruby Trivia 3

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

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

  4. How can you list all
 global variables? Bonus Question:

  5. How can you list all
 global variables? Bonus Question: Answer:

    Use the Kernel#global_variables method.
  6. How many global variables does Ruby define? global_variables.count Bonus Question:

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

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

  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:
  10. What does Ruby’s -p switch do? Bonus Question:

  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:
  12. What thread-local variable can only store four possible values? Question

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

    3: Hint #1: Those values are 0, 1, 2, and 3.
  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.
  15. $SAFE Answer 3: Trick question because it looks like a

    global variable, even though it behaves like a thread-local variable.
  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.
  17. How can you check if an object is trusted? Bonus

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

    Question: Answer: Use the Kernel#tainted? method.
  19. What happens if you do this? module Kernel def tainted?

    return false end end Question 4:
  20. The tainted? method will always return false, but Ruby will

    still track tainted state via an internal FL_TAINT flag. Answer 4:
  21. How can you mark a
 tainted object as safe? Question

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

    Answer 5:
  23. Thanks for playing! Follow @sferik on Twitter for more Ruby

    trivia and practica.