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

Ruby Quiz

pocke
October 08, 2017

Ruby Quiz

at 学生エンジニア限定LT大会

pocke

October 08, 2017
Tweet

More Decks by pocke

Other Decks in Programming

Transcript

  1. Ruby Quiz
    Oct. 8, 2017
    第4回 学生エンジニア限定LT大会

    View Slide

  2. I have only 5 minutes,
    so I'll speak very fast.

    View Slide

  3. Self introduction
    ● Pocke
    ● Actcat, Inc. / SideCI
    ● RuboCop' s core developer
    ● Student at The Open University of Japan

    View Slide

  4. SideCI

    View Slide

  5. Automated Code Review
    as a Service

    View Slide

  6. Please access here! https://sideci.com

    View Slide

  7. Ruby Quiz

    View Slide

  8. Question

    View Slide

  9. Do you write Ruby?

    View Slide

  10. Ruby Quiz
    ● You are a Ruby interpreter.
    ○ You are a parse.y.
    ○ You are a YARV.
    ● You execute ruby code, and you output result.

    View Slide

  11. Quiz 1

    View Slide

  12. Q. What does the code output?
    p(??)
    p(%_?_)

    View Slide

  13. Answer
    p(??) # => "?"
    p(%_?_) # => "?"

    View Slide

  14. Why?

    View Slide

  15. A. Why does it output "?"?
    p(??)
    p(%_?_)
    %…
    is a string literal.
    e.g.) %_?_ == "?" , %!???! == "???"
    ?_ is a character literal.
    e.g.) ?_ == "_" , ?? == "?"

    View Slide

  16. Quiz 2

    View Slide

  17. Q. What does the code output?
    p(????::?:)
    p(% %s% %%%%)

    View Slide

  18. Answer
    p(????::?:) # => ":"
    p(% %s% %%%%) # => "%"

    View Slide

  19. Why????

    View Slide

  20. Answer
    p(????::?:)
    p(% %s% %%%%)
    1. ?? is "?"
    2. ? is a part of conditional op
    3. ?: is ":"
    4. : is a part of conditional op
    5. ?: is ":"
    "?" ? ":" : ":"
    1. % %s% is "%s%"
    2. % is a format op(Like sprintf)
    3. %%% is ""
    sprintf("%s%", "")

    View Slide

  21. Quiz 3

    View Slide

  22. Q. What does the code output?
    a = ["1","2","3"]
    a&.map(&:to_i)&.&([1])

    View Slide

  23. Answer
    a = ["1","2","3"]
    a&.map(&:to_i)&.&([1])
    # => [1]

    View Slide

  24. Why?????

    View Slide

  25. Answer
    a = ["1","2","3"]
    a&.map(&:to_i)&.&([1])
    # => [1]
    &. is a safe navigation op(nil guard).
    &:to_i is an implicit to_proc
    op.
    A safe
    navigation op & is an intersection op call
    (like [1,2,3] & [1])

    View Slide

  26. Conclusion

    View Slide

  27. ● We can write tricky code in Ruby.
    ○ Very fun!!!
    Thank you for listening!
    Conclusion

    View Slide