I Spoke at RubyConfIndia 2016 about Closer look at method scopes in ruby.
In this talk I did code walk through of MRI implementation of ruby(ruby 2.2). And important data structure "rb_control_frame_t".
http://rubyconfindia.org/
4. class Foo
def public_method
puts "I'm Public"
end
protected
def protected_method
puts "I'm protected"
end
private
def private_method
puts "I'm private"
end
end
5. > foo = Foo.new
> foo.public_method
#=> I'm Public
8. class Bar < Foo
end
> bar = Bar.new
> bar.public_method
#=> I'm Public
> bar.protected_method
#=> NoMethodError: protected method `protected_method'
called for #<Foo>
> bar.private_method
#=>NoMethodError: private method `private_method' called for
#<Foo>
24. /* vm.c */
static void
vm_define_method(rb_thread_t *th, VALUE obj, ID id,
VALUE iseqval, int is_singleton)
{
VALUE klass;
rb_method_visibility_t visi;
rb_cref_t *cref = rb_vm_cref();
if (!is_singleton) {
klass = CREF_CLASS(cref);
visi = rb_scope_visibility_get();
}
else { /* singleton */
/* class and frozen checked in this API */
klass = rb_singleton_class(obj);
visi = METHOD_VISI_PUBLIC;
}
…
}
27. class AnyClass
class << self
def any_public_method
end
protected
def any_protected_method
end
private
def any_private_method
end
end
end
CFP
[AnyClass]
CFP
CFP
CFP
[self]
Ruby call stack