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

May the source be with you

高見龍
December 08, 2012

May the source be with you

Learn more about Ruby through reading Ruby source code.

高見龍

December 08, 2012
Tweet

More Decks by 高見龍

Other Decks in Programming

Transcript

  1. May the Source Be with You
    Chimpr
    Learning Ruby by Reading Ruby Source Code
    12年12月8日星期六

    View Slide

  2. 12年12月8日星期六

    View Slide

  3. I’m a Flash guy
    ≈ 8 years
    12年12月8日星期六

    View Slide

  4. also a Ruby guy
    ≈ 4 years
    12年12月8日星期六

    View Slide

  5. but not a C guy, yet!
    12年12月8日星期六

    View Slide

  6. Ruby > Rails
    12年12月8日星期六

    View Slide

  7. share Ruby learning experiences in
    OpenFoundry and some developer party.
    12年12月8日星期六

    View Slide

  8. a SHOW-OFF person!
    12年12月8日星期六

    View Slide

  9. Learning Ruby
    by Reading Ruby Source Code
    12年12月8日星期六

    View Slide

  10. - why read source code?
    - how o sart?
    - anything ineresting?
    - wrie a simple Ruby Exension with C.
    12年12月8日星期六

    View Slide

  11. Why read source code?
    12年12月8日星期六

    View Slide

  12. see how the Ruby Core Team wrie Ruby
    12年12月8日星期六

    View Slide

  13. fun :)
    12年12月8日星期六

    View Slide

  14. "once you sart digging around in someone else’s
    code base, you’ll learn a lot about your own
    strengths and weaknesses" - Ruby Best Practice
    http://blog.rubybestpractices.com/posts/gregory/005-code-reading-stdlib.html
    12年12月8日星期六

    View Slide

  15. pre-requirement?
    12年12月8日星期六

    View Slide

  16. C language
    12年12月8日星期六

    View Slide

  17. curiosity and enthusiasm
    12年12月8日星期六

    View Slide

  18. What if I don’t have any skill of C?
    12年12月8日星期六

    View Slide

  19. Just learn it :)
    12年12月8日星期六

    View Slide

  20. How o Sart?
    12年12月8日星期六

    View Slide

  21. get Ruby source code
    download from Ruby websie:
    http://www.ruby-lang.org/en/
    or from github:
    https://github.com/ruby/ruby
    12年12月8日星期六

    View Slide

  22. Ruby Source Code:
    - *.c + *.h
    - *.rb (Sdlib)
    12年12月8日星期六

    View Slide

  23. Tools
    - Vim
    12年12月8日星期六

    View Slide

  24. Ruby 1.9.2 p290
    12年12月8日星期六

    View Slide

  25. Quick Browsing.. :)
    12年12月8日星期六

    View Slide

  26. what's a Ruby object?
    12年12月8日星期六

    View Slide

  27. sart from basic structure, like Object,
    Class, String, Array, Hash, Basic...
    12年12月8日星期六

    View Slide

  28. Object = RObject,
    Class = RClass,
    String = RString,
    Array = RArray,
    Hash = RHash,
    Basic = RBasic
    12年12月8日星期六

    View Slide

  29. “ruby.h”
    12年12月8日星期六

    View Slide

  30. “object.c”
    12年12月8日星期六

    View Slide

  31. Init_XXXX
    12年12月8日星期六

    View Slide

  32. class.c#350-354
    all Class’s class is Class!
    12年12月8日星期六

    View Slide

  33. class.c#556
    rb_define_module()
    12年12月8日星期六

    View Slide

  34. class.c#1170
    rb_define_method()
    12年12月8日星期六

    View Slide

  35. Object.c#2556~2560
    Integer(), Float(), String(), Array() are all
    global methods
    12年12月8日星期六

    View Slide

  36. = rb_define_module_method on kernel
    = rb_define_private_method on kernel +
    rb_define_singleton_method on kernel
    class.c#1332
    rb_define_global_function()
    12年12月8日星期六

    View Slide

  37. “new”
    12年12月8日星期六

    View Slide

  38. class A
    def initialize
    puts "hello"
    end
    end
    a = A.new
    12年12月8日星期六

    View Slide

  39. object.c#2624
    new ->
    rb_class_new_instance ->
    rb_obj_alloc ->
    rb_obj_call_init
    12年12月8日星期六

    View Slide

  40. o_s v.s o_str
    12年12月8日星期六

    View Slide

  41. String.c#7488
    rb_define_method(rb_cString, "to_s", rb_str_to_s, 0);
    rb_define_method(rb_cString, "to_str", rb_str_to_s, 0);
    12年12月8日星期六

    View Slide

  42. Array.c#4504
    rb_define_alias(rb_cArray, "to_s", "inspect");
    but no `to_str`
    12年12月8日星期六

    View Slide

  43. proc.c#2105
    how to execute a Proc?
    - proc.call
    - proc[]
    - proc === 123
    - proc.yield
    12年12月8日星期六

    View Slide

  44. object.c#2592
    - attr
    - attr_reader
    - attr_writer
    - attr_accessor
    12年12月8日星期六

    View Slide

  45. push v.s <<
    12年12月8日星期六

    View Slide

  46. array.c#709 (<<)
    VALUE
    rb_ary_push(VALUE ary, VALUE item){
    rb_ary_modify(ary);
    return rb_ary_push_1(ary, item);
    }
    12年12月8日星期六

    View Slide

  47. array.c#742 (push)
    static VALUE
    rb_ary_push_m(int argc, VALUE *argv, VALUE
    ary) {
    rb_ary_modify(ary);
    while (argc--) {
    rb_ary_push_1(ary, *argv++);
    }
    return ary;
    }
    12年12月8日星期六

    View Slide

  48. gc.c#3214-3215
    Object’s ID
    12年12月8日星期六

    View Slide

  49. gc.c#2865-2867
    How Symbol works?
    12年12月8日星期六

    View Slide

  50. people = {
    "Eddie" => ["green", "[email protected]"],
    "Joanne" => ["yellow", "[email protected]"]
    }
    people.map { |name, (color, email)|
    puts [name, email]
    }
    12年12月8日星期六

    View Slide

  51. people = {
    "Eddie" => ["green", "[email protected]"],
    "Joanne" => ["yellow", "[email protected]"]
    }
    people.map { |name, (_, email)|
    puts [name, email]
    }
    parse.y#8277-8299 shadowing_lvar_gen()
    12年12月8日星期六

    View Slide

  52. vm_eval.c#224
    method_missing
    12年12月8日星期六

    View Slide

  53. ineresting naming :)
    12年12月8日星期六

    View Slide

  54. ext/ripper/ripper.c#15890
    rb_intern, rb_intern2, rb_intern3
    12年12月8日星期六

    View Slide

  55. array.c#325
    rb_ary_new, rb_ary_new2, ary_new,
    rb_ary_new3, rb_ary_new4
    12年12月8日星期六

    View Slide

  56. Simple Ruby Exension from
    Scrach
    12年12月8日星期六

    View Slide

  57. Conclusion
    12年12月8日星期六

    View Slide

  58. from basic structure
    12年12月8日星期六

    View Slide

  59. and don’t be afraid
    12年12月8日星期六

    View Slide

  60. References
    12年12月8日星期六

    View Slide

  61. Rubyソースコード完全解説
    by 青木峰郎
    http://i.loveruby.net/ja/rhg/book/
    12年12月8日星期六

    View Slide

  62. Ruby Under a Microscope
    by Pat Shaughnessy
    http://patshaughnessy.net/ruby-under-a-microscope
    12年12月8日星期六

    View Slide

  63. still not finish..
    12年12月8日星期六

    View Slide

  64. Ian Ruotsala
    12年12月8日星期六

    View Slide

  65. Chimpr
    Hope can become a
    Jedi Master someday :)
    12年12月8日星期六

    View Slide

  66. おわり
    thank you all :)
    12年12月8日星期六

    View Slide

  67. any question?
    12年12月8日星期六

    View Slide

  68. 高見見龍龍
    Conacts
    photo by Eddie
    Websie
    Blog
    Plurk
    Facebook
    Google Plus
    Twiter
    Email
    Mobile
    http://www.eddie.com.tw
    http://blog.eddie.com.tw
    http://www.plurk.com/aquarianboy
    http://www.facebook.com/eddiekao
    http://www.eddie.com.tw/+
    https://twiter.com/#!/eddiekao
    [email protected]
    +886-928-617-687
    12年12月8日星期六

    View Slide