28
Ruby is Awesome!

Ruby Conf India 2016

Embed Size (px)

Citation preview

Page 1: Ruby Conf India 2016

Ruby is Awesome!

Page 2: Ruby Conf India 2016

–Sumit Mahamuni@meetmahamuni

https://github.com/sumitmah

Page 3: Ruby Conf India 2016

Some Background about the talk

Page 4: Ruby Conf India 2016

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" endend

Page 5: Ruby Conf India 2016

> foo = Foo.new> foo.public_method #=> I'm Public

Page 6: Ruby Conf India 2016

> foo.protected_method > NoMethodError: protected method `protected_method' called for #<Foo>

Page 7: Ruby Conf India 2016

> foo.private_method #=>NoMethodError: private method `private_method' called for #<Foo>

Page 8: Ruby Conf India 2016

class Bar < Fooend

> 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>

Page 9: Ruby Conf India 2016

class Foo private def private_method puts "I'm private" end

def self.private_method puts "I'm private" endend

Page 10: Ruby Conf India 2016

> foo = Foo.new> foo.private_method #=> NoMethodError: private method `private_method' called for #<Foo>

Page 11: Ruby Conf India 2016

> Foo.private_method #=> I'm private

Page 12: Ruby Conf India 2016

foom_table

Foo

instance_private_method

#<Class:Foo> m_table

class_private_methodclass Foo private def instance_private_method puts "I'm private" end

def self.class_private_method puts "I'm private" endend

Page 13: Ruby Conf India 2016

What is “private”?

Page 14: Ruby Conf India 2016

/* vm_method.c */voidInit_eval_method(void){ ... rb_define_private_method(rb_cModule, “private", rb_mod_private, -1); ...}

Page 15: Ruby Conf India 2016

/* class.c */

static VALUEboot_defclass(const char *name, VALUE super);

voidInit_class_hierarchy(void){ rb_cBasicObject = boot_defclass("BasicObject", 0); rb_cObject = boot_defclass("Object", rb_cBasicObject); rb_cModule = boot_defclass("Module", rb_cObject); rb_cClass = boot_defclass("Class", rb_cModule); …}

Page 16: Ruby Conf India 2016

Object structure

> class A> end => nil

> A.class => Class

> A.class.ancestors => [Class, Module, Object, Kernel, BasicObject]

Page 17: Ruby Conf India 2016

class WhatIsPrivate privateend

Page 18: Ruby Conf India 2016

/* vm_method.c */static voidvm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func){ rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t*)&rb_vm_cref()->scope_visi;

scope_visi->method_visi = method_visi; scope_visi->module_func = module_func;}

Page 19: Ruby Conf India 2016

/* vm.c */rb_cref_t *rb_vm_cref(void){ rb_thread_t *th = GET_THREAD(); rb_control_frame_t *cfp =

rb_vm_get_ruby_level_next_cfp(th, th->cfp);

if (cfp == NULL) { return NULL; } return rb_vm_get_cref(cfp->ep);}

Page 20: Ruby Conf India 2016

This image is taken from Ruby under microscope book.

Page 21: Ruby Conf India 2016

This image is taken from Ruby under microscope book.

Page 22: Ruby Conf India 2016

class WhatIsDefineMethod private def i_am_private end def self.method_defined_on_self_objectendend

Page 23: Ruby Conf India 2016

static voidvm_define_method(rb_thread_t *th, VALUE obj, ID id, VALUE iseqval, int is_singleton);

Page 24: Ruby Conf India 2016

/* vm.c */

static voidvm_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; } …}

Page 25: Ruby Conf India 2016

static rb_method_visibility_trb_scope_visibility_get(void){ rb_thread_t *th = GET_THREAD(); rb_control_frame_t *cfp =

rb_vm_get_ruby_level_next_cfp(th, th->cfp);

if (!vm_env_cref_by_cref(cfp->ep)) { return METHOD_VISI_PUBLIC; } else { return

CREF_SCOPE_VISI(rb_vm_cref())->method_visi; }}

Page 26: Ruby Conf India 2016

object

class WhatIsDefineMethod private def i_am_private end def self.method_defined_on_self_object endend

m_table

WhatIsDefineMethod

i_am_private

#<Class:WhatIsDefineMethod>

m_table

method_defined_on_self_object

Page 27: Ruby Conf India 2016

class AnyClass class << self def any_public_method end

protected def any_protected_method end

private def any_private_method end endend

CFP[AnyClass]

CFP

CFP

CFP[self]

Ruby call stack

Page 28: Ruby Conf India 2016

–Sumit Mahamuni@meetmahamuni

https://github.com/sumitmah

“Thanks.”