Slide 1

Slide 1 text

Ruby Trivia 3

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

The String last read by gets. Answer 1:

Slide 4

Slide 4 text

How can you list all
 global variables? Bonus Question:

Slide 5

Slide 5 text

How can you list all
 global variables? Bonus Question: Answer: Use the Kernel#global_variables method.

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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:

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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:

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

$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.

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

How can you mark a
 tainted object as safe? Question 5:

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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