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

Dissecting Ruby

bostonrb
September 09, 2012

Dissecting Ruby

bostonrb

September 09, 2012
Tweet

More Decks by bostonrb

Other Decks in Programming

Transcript

  1. rb_block_t iseq putstring "The quick brown fox… " setdynamic str,

    0 putself getdynamic str, 0 send :puts, 1 leave
  2. str = "The quick brown fox" 10.times do str2 =

    "jumps over the lazy dog." puts "#{str} #{str2}" end
  3. rb_block_t iseq DFP putstring "jumps over the lazy dog." setdynamic

    str, 0 putself getdynamic str2, 1 tostring putstring " " getdynamic str, 0 tostring concatstrings 3 send :puts, 1, nil, 8, <ic:0> leave YARV internal stack locals: str
  4. “In computer science, a closure (also lexical closure or function

    closure) is a function or reference to a function together with a referencing environment...” Sussman and Steele. Scheme: An interpreter for extended lambda calculus
  5. def display_message str = "The quick brown fox" lambda do

    str2 = "jumps over the lazy dog" puts "#{str} #{str2}" end end display_message.call
  6. def display_message str = "The quick brown fox" lambda do

    str2 = "jumps over the lazy dog" puts "#{str} #{str2}" end end display_message.call
  7. class Quote def initialize @str = "The quick brown fox..."

    end def display_message puts @str end end
  8. class Quote def initialize @str = "The quick brown fox..."

    end define_method :display_message do puts @str end end
  9. class Quote def initialize @str = "The quick brown fox"

    end end str2 = "jumps over the lazy dog." Quote.send(:define_method, :display_message) do puts "#{@str} #{str2}" end
  10. str = "puts" str += " 2" str += "

    +" str += " 2" eval(str) => 4
  11. rb_control_frame_t LFP self rb_binding_t filename line_no env rb_env_t rb_block_t iseq

    DFP env self Stack Heap RObject ivptr klass @str etc...
  12. putstring "The quick brown fox… " setdynamic str, 0 putself

    getdynamic str, 0 send :puts, 1 leave YARV internal stack locals: str RObject ivptr klass rb_block_t DFP self iseq
  13. str2 = "jumps over the lazy dog." obj = Quote.new

    obj.instance_eval do puts "#{@str} #{str2}" end
  14. obj.display_message The quick brown fox jumps over the lazy dog.

    Quote.new.display_message ...undefined method `display_message' for #<Quote:0x007fdf789504e8> (NoMethodError)
  15. RClass super RClass super Object (class) Kernel (module) Quote (class)

    RClass super RClass super BasicObject (class) RObject klass obj (object)
  16. etc... RClass super RClass super Object (class) obj (object) RObject

    klass SomeClass (class) RClass super #<Class:#<Quote:0x007f9ed9150c28>> (singleton)