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

Ruby Trivia 2

Erik Berlin
November 05, 2015

Ruby Trivia 2

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

Erik Berlin

November 05, 2015
Tweet

More Decks by Erik Berlin

Other Decks in Programming

Transcript

  1. Ruby
    Trivia
    2

    View Slide

  2. What is the return value of Ruby’s
    method visibility keywords?
    private, public, protected
    Question 1:

    View Slide

  3. self
    Answer 1:
    Ruby’s method visibility keywords can be used as r-values.

    View Slide

  4. Given a button method that takes

    an argument and a block,

    which of these are valid Ruby?
    button "string" { |n| n }
    button "string" do |n| n end
    button ["string"] { |n| n }
    button ["string"] do |n| n end
    Question 2:

    View Slide

  5. All except:

    button("string") { |n| n }
    Answer 2:

    View Slide

  6. How could this line be fixed?
    button("string") { |n| n }
    Bonus Question:

    View Slide

  7. How could this line be fixed?
    button("string") { |n| n }
    Bonus Question:
    Answer: Add parentheses around the argument.

    View Slide

  8. What is the result of this line:
    a Hash with a String key,
    a Hash with a Symbol key,

    or a SyntaxError?
    {"key": "What happens?"}
    Question 3:

    View Slide

  9. a Hash with

    a Symbol key*
    Answer 3:
    *In Ruby 2.2 and later.
    In earlier versions, it raises a SyntaxError.

    View Slide

  10. What is the value of each argument?

    def foo(*w, a: 7, **t)
    puts "w: #{w}, a: #{a}, t: #{t}"
    end
    foo({a: 1, b: 2, "c" => 3, d: 4})
    Question 4:

    View Slide

  11. w: [{"c" => 3}]
    a: 1
    t: {:b => 2, :d => 4}
    Answer 4:

    View Slide

  12. What happens if you add a positional
    argument to the end?
    def foo(*w, a: 7, **t)
    puts "w: #{w}, a: #{a}, t: #{t}"
    end
    foo({a: 1, b: 2, "c" => 3, d: 4}, 5)
    Bonus Question:

    View Slide

  13. What happens if you add a positional
    argument to the end?
    w: [{:a=>1, :b=>2, "c"=>3, :d=>4}, 5]
    a: 7
    t: {}
    Bonus Question:

    View Slide

  14. When you inspect Object.new,

    Ruby outputs something like:
    #
    What are the properties and
    significance of this hex number?
    Question 5:

    View Slide

  15. 1. It’s the object’s address in memory.
    2. It’s always even.
    3. It’s double the object’s object_id.


    Answer 5:

    View Slide

  16. Answer 5:

    View Slide

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

    View Slide