$30 off During Our Annual Pro Sale. View Details »

Ruby Trivia

Erik Berlin
October 01, 2015

Ruby Trivia

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

Erik Berlin

October 01, 2015
Tweet

More Decks by Erik Berlin

Other Decks in Programming

Transcript

  1. Ruby
    Trivia

    View Slide

  2. In Ruby, a dash may not be used in a
    variable name…
    …but before Ruby 2.1, one variable
    was allowed to be named with a dash.
    What was it called?
    Question 1:

    View Slide

  3. In Ruby, a dash may not be used in a
    variable name…
    …but before Ruby 2.1, one variable
    was allowed to be named with a dash.
    What was it called?
    Hint: It’s a global variable.
    Question 1:

    View Slide

  4. $-
    Answer 1:

    View Slide

  5. Why can’t variable names
    contain dashes?
    Bonus Question:

    View Slide

  6. Why can’t variable names
    contain dashes?
    Bonus Question:
    Answer: Because the Ruby parser can’t distinguish it from the
    minus (-) operator between two variables (e.g. forty-two).

    View Slide

  7. Ruby may define a special File
    object in the first file it interprets.
    What is it called?
    What does it do?
    Question 2:

    View Slide

  8. Ruby may define a special File
    object in the first file it interprets.
    What is it called?
    Question 2:
    Hint: When you call read, it reads the file, starting after
    __END__

    View Slide

  9. DATA
    Answer 2:

    View Slide

  10. What happens if you call:
    DATA.rewind
    before
    DATA.read
    Bonus Question:

    View Slide

  11. What happens if you call:
    DATA.rewind
    before
    DATA.read
    Bonus Question:
    Answer: It reads the entire file, including the source code.

    View Slide

  12. The following line of code is
    valid Ruby syntax:
    a = true && return

    But this line of code is not:
    a = return && true
    What is the error message?
    Question 3:

    View Slide

  13. void value
    expression
    Answer 3:

    View Slide

  14. When you convert the range:
    (1..3).to_a
    You get the array:

    [1, 2, 3]

    What is the result of:
    (3..1).to_a
    Question 4:

    View Slide

  15. []
    Answer 4:

    View Slide

  16. What method would need to be
    added to the Range interface to
    make descending sequences
    possible?
    Bonus Question:

    View Slide

  17. What method would need to be
    added to the Range interface to
    make descending sequences
    possible?
    Bonus Question:
    Answer: pred (the opposite of succ).

    View Slide

  18. What Ruby class mixes-in
    Comparable but doesn’t define
    the spaceship (<=>) operator?
    Question 5:

    View Slide

  19. What Ruby class mixes-in
    Comparable but doesn’t define
    the spaceship (<=>) operator?
    Hint: It’s a descendant of Numeric.
    Question 5:

    View Slide

  20. Complex
    Answer 5:

    View Slide

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

    View Slide