module Exlabs
class Presentation
attr_reader :author, :title, :date
def self.ish
new(author: 'Wojtek Widenka',
title: 'Selfish Presentation')
end
def initialize(params)
@author = params.fetch(:author, 'unknown')
@title = params.fetch(:title, 'unknown')
@date = params.fetch(:date, Date.today)
end
end
end
module Exlabs
class Presentation
attr_reader :author, :title, :date
puts "1: #{self}"
def self.ish
puts "2: #{self}"
new(author: 'Wojtek Widenka',
title: 'Selfish Presentation')
end
def initialize(params = {})
@author = params.fetch(:author, 'unknown')
@title = params.fetch(:title, 'unknown')
@date = params.fetch(:date, Date.today)
puts "3: #{self}"
end
def foo
puts "4: #{self}"
end
end
end
module Exlabs
class Presentation
attr_reader :author, :title, :date
puts "1: #{self}t#{self.class}"
def self.ish
puts "2: #{self}t#{self.class}"
new(author: 'Wojtek Widenka',
title: 'Selfish Presentation')
end
def initialize(params = {})
@author = params.fetch(:author, 'unknown')
@title = params.fetch(:title, 'unknown')
@date = params.fetch(:date, Date.today)
puts "3: #{self}t#{self.class}"
end
def foo
puts "4: #{self}t#{self.class}"
end
end
end
# enter the text from left side
1: Exlabs::Presentation Class
=> :foo
> object = Exlabs::Presentation.ish
2: Exlabs::Presentation Class
3: #<Exlabs::Presentation:0x007f8fa9eac028> Exlabs::Presentation
=> #<Exlabs::Presentation:0x007f8fa9eac028 @author="Wojtek
Widenka", @title="Selfish Presentation", @date=#<Date: 2016-10-26
((2457688j,0s,0n),+0s,2299161j)>>
> object.foo
4: #<Exlabs::Presentation:0x007f8fa9eac028> Exlabs::Presentation
=> nil
SystemStackError: stack level too deep
def self.h
@h = 'st'
puts h
end
module Exlabs
class Examples
->
end
end
def self.a
puts a
end
def self.b
puts self.b
end
st
=> nil
def self.c
c = 'st'
puts c
end
def self.d
d = 'st'
puts self.d
end
def self.e
self.e = 'st'
puts e
end
def self.f
self.f = 'st'
puts self.f
end
def self.g
@g = 'st'
puts @g
end
> Exlabs::Examples.a
NoMethodError: undefined method `e='
for Exlabs::Examples:Class
def self.i
@i = 'st'
puts self.i
end
SystemStackError: stack level too deep
def q
@q = 'st'
puts q
end
module Exlabs
class Examples
->
end
end
def j
puts j
end
def k
puts self.k
end
st
=> nil
def l
l = 'st'
puts l
end
def m
m = 'st'
puts self.m
end
def n
self.n = 'st'
puts n
end
def o
self.o = 'st'
puts self.o
end
def p
@p = 'st'
puts @p
end
> Exlabs::Examples.new.j
NoMethodError: undefined method `n=' for
#<Exlabs::Examples:0x007f8130af82d8>
def r
@r = 'st'
puts self.r
end
SystemStackError: stack level too deep
def self.h
@h = 'st'
puts h
end
module Exlabs
class Examples
->
end
end
def self.a
puts a
end
def self.b
puts self.b
end
st
=> nil
def self.c
c = 'st'
puts c
end
def self.d
d = 'st'
puts self.d
end
def self.e
self.e = 'st'
puts e
end
def self.f
self.f = 'st'
puts self.f
end
def self.g
@g = 'st'
puts @g
end
> Exlabs::Examples.a
NoMethodError: undefined method `e='
for Exlabs::Examples:Class
def self.i
@i = 'st'
puts self.i
end
SystemStackError: stack level too deep
def q
@q = 'st'
puts q
end
module Exlabs
class Examples
->
end
end
def j
puts j
end
def k
puts self.k
end
st
=> nil
def l
l = 'st'
puts l
end
def m
m = 'st'
puts self.m
end
def n
self.n = 'st'
puts n
end
def o
self.o = 'st'
puts self.o
end
def p
@p = 'st'
puts @p
end
> Exlabs::Examples.new.j
NoMethodError: undefined method `n=' for
#<Exlabs::Examples:0x007f8130af82d8>
def r
@r = 'st'
puts self.r
end
module Exlabs
class Presentation
attr_reader :author, :title, :date
puts "1: #{self}t#{self.class}"
def self.ish
puts "2: #{self}t#{self.class}"
new(author: 'Wojtek Widenka',
title: 'Selfish Presentation')
end
class << self
puts "5: #{self}t#{self.class}"
def bar
puts "6: #{self}t#{self.class}"
end
def self.foobar
puts "7: #{self}t#{self.class}"
end
end
def initialize(params = {})
@author = params.fetch(:author, 'unknown')
@title = params.fetch(:title, 'unknown')
@date = params.fetch(:date, Date.today)
puts "3: #{self}t#{self.class}"
end
def foo
puts "4: #{self}t#{self.class}"
end
end
end
# enter the text from left side
1: Exlabs::Presentation Class
5: #<Class:Exlabs::Presentation> Class
=> :foo
> object = Exlabs::Presentation.ish
2: Exlabs::PresentationClass
3: #<Exlabs::Presentation:0x007fce5514b218> Exlabs::Presentation
=> #<Exlabs::Presentation:0x007fce5514b218 @author=[…]>
> object.foo
4: #<Exlabs::Presentation:0x007fce5514b218> Exlabs::Presentation
=> nil
> object.bar
NoMethodError: undefined method `bar' for #<Exlabs::Presentation:0x007fce5514b218>
> Exlabs::Presentation.bar
6: Exlabs::Presentation Class
=> nil
> object.foobar
NoMethodError: undefined method `foobar' for #<Exlabs::Presentation:0x007fce5514b218>
> Exlabs::Presentation.foobar
NoMethodError: undefined method `foobar' for Exlabs::Presentation:Class
> Exlabs::Presentation.singleton_class.foobar
7: #<Class:Exlabs::Presentation> Class
=> nil
module Exlabs
class Presentation
attr_reader :author, :title, :date
puts "1: #{self}t#{self.class}"
def self.ish
puts "2: #{self}t#{self.class}"
new(author: 'Wojtek Widenka',
title: 'Selfish Presentation')
end
class << self
puts "5: #{self}t#{self.class}"
def bar
puts "6: #{self}t#{self.class}"
end
def self.foobar
puts "7: #{self}t#{self.class}"
end
end
def initialize(params = {})
@author = params.fetch(:author, 'unknown')
@title = params.fetch(:title, 'unknown')
@date = params.fetch(:date, Date.today)
puts "3: #{self}t#{self.class}"
end
def foo
puts "4: #{self}t#{self.class}"
end
end
end
module Exlabs
class Stranger
class << self
puts "8: #{self}t#{self.class}"
def bar
puts "9: #{self}t#{self.class}"
end
private
def private_method
puts 'Just a text'
end
end
class << Presentation
puts "10: #{self}t#{self.class}"
def bar
puts "11: #{self}t#{self.class}"
end
end
end
end
# enter the text from left side
8: #<Class:Exlabs::Stranger> Class
10: #<Class:Exlabs::Presentation> Class
=> :bar
> Exlabs::Stranger.bar
9: Exlabs::Stranger Class
=> nil
> Exlabs::Stranger.private_method
NoMethodError: private method `private_method'
called for Exlabs::Stranger:Class
> Exlabs::Presentation.bar
11: Exlabs::Presentation Class
=> nil
str1 = 'String 1'
str2 = 'String 2'
class << String
def foo
puts 'foo called'
puts self
end
end
class << str1
def bar
puts 'bar called'
puts self
end
end
> str1.foo
NoMethodError: undefined method `foo' for "String 1":String
> String.foo
foo called
String
=> nil
> str1.bar
bar called
String 1
=> nil
> str2.bar
NoMethodError: undefined method `bar' for "String 2":String
> String.singleton_class.method_defined? :bar
=> false
> str1.singleton_class.method_defined? :bar
=> true
> str2.singleton_class.method_defined? :bar
=> false
str1 = 'String 1'
str2 = 'String 2'
class << String
def foo
puts 'foo called'
puts self
end
end
class << str1
def bar
puts 'bar called'
puts self
end
end
> str1.foo
NoMethodError: undefined method `foo' for "String 1":String
> String.foo
foo called
String
=> nil
> str1.bar
bar called
String 1
=> nil
> str2.bar
NoMethodError: undefined method `bar' for "String 2":String
> String.singleton_class.method_defined? :bar
=> false
> str1.singleton_class.method_defined? :bar
=> true
> str2.singleton_class.method_defined? :bar
=> false
▸ ruby is tricky
▸ in ruby everything is an object
▸ from ruby implementation point of view Class is slightly different than Object
struct RObject {
struct RBasic basic;
union {
struct {
uint32_t numiv;
VALUE *ivptr;
void *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
struct RBasic {
VALUE flags;
const VALUE klass;
}
struct RClass {
struct RBasic basic;
VALUE super;
rb_classext_t *ptr;
struct rb_id_table *m_tbl;
};
▸ classes contains methods while objects variables (this is confusing, not sure if
can explain)
▸ when we implementing new class
class NewClass
def new_method
end
end
▸ we are creating new instance of Class (we can use instead Class.new syntax)
▸ There is also constructed singleton class (eigenclass, metaclass) in which methods
are stored
▸ Since it is singleton it cannot be duplicated and is connected always with one object
▸ ruby is tricky, metaclass is transparent, so calling #class on object will ommit it
"Nathan".is_a?(BasicObject) # => true "Nathan" is an object.
"Nathan".class #=> String "Nathan" is an instance of the String class.
"Nathan".is_a?(Class) #=> false "Nathan" is not a class.
"Nathan".superclass # NoMethodError "Nathan" has no superclass, because "Nathan" is not a class.
String.is_a?(BasicObject) #=> true String is an object.
String.class #=> Class String is an instance of the Class class.
String.superclass #=> Object String's superclass is Object.
Object.is_a?(BasicObject) #=> true Object is an object.
Object.class #=> Class Object is an instance of the Class class.
Object.superclass #=> BasicObject Object's superclass is BasicObject.
BasicObject.is_a?(BasicObject) #=> true BasicObject is an object.
BasicObject.class #=> Class BasicObject is an instance of the Class class
BasicObject.superclass #=> nil BasicObject's superclass is nil.
nil.is_a?(BasicObject) #=> true nil is an object.
nil.class #=> NilClass nil is an instance of the NilClass class
nil.superclass # NoMethodError nil has no superclass, because it is not a class.
Class.is_a?(BasicObject) #=> true Class is an object.
Class.class #=> Class Class is an instance of the Class class.
# this is probably the most important part.
Class.superclass #=> Module Class's superclass is Module
# 2nd most important part
Module.is_a?(BasicObject) #=> true Module is an object
Module.class #=> Class Module is an instance of the Class class. # 3rd
Module.superclass #=> Object Module's superclass is Object # 4th
Object.is_a?(BasicObject) #=> true Object is an object.
Object.class #=> Class Object is an instance of the Class class.
Object.superclass #=> BasicObject Object's superclass is BasicObject.
BasicObject.is_a?(BasicObject) #=> true BasicObject is an object.
BasicObject.class #=> Class BasicObject is an instance of the Class class
BasicObject.superclass #=> nil BasicObject's superclass is nil.
nil.is_a?(BasicObject) #=> true nil is an object.
nil.class #=> NilClass nil is an instance of the NilClass class
nil.superclass # NoMethodError nil has no superclass, because it is not a class.

Selfish presentation - ruby internals

  • 1.
    module Exlabs class Presentation attr_reader:author, :title, :date def self.ish new(author: 'Wojtek Widenka', title: 'Selfish Presentation') end def initialize(params) @author = params.fetch(:author, 'unknown') @title = params.fetch(:title, 'unknown') @date = params.fetch(:date, Date.today) end end end
  • 2.
    module Exlabs class Presentation attr_reader:author, :title, :date puts "1: #{self}" def self.ish puts "2: #{self}" new(author: 'Wojtek Widenka', title: 'Selfish Presentation') end def initialize(params = {}) @author = params.fetch(:author, 'unknown') @title = params.fetch(:title, 'unknown') @date = params.fetch(:date, Date.today) puts "3: #{self}" end def foo puts "4: #{self}" end end end
  • 3.
    module Exlabs class Presentation attr_reader:author, :title, :date puts "1: #{self}t#{self.class}" def self.ish puts "2: #{self}t#{self.class}" new(author: 'Wojtek Widenka', title: 'Selfish Presentation') end def initialize(params = {}) @author = params.fetch(:author, 'unknown') @title = params.fetch(:title, 'unknown') @date = params.fetch(:date, Date.today) puts "3: #{self}t#{self.class}" end def foo puts "4: #{self}t#{self.class}" end end end # enter the text from left side 1: Exlabs::Presentation Class => :foo > object = Exlabs::Presentation.ish 2: Exlabs::Presentation Class 3: #<Exlabs::Presentation:0x007f8fa9eac028> Exlabs::Presentation => #<Exlabs::Presentation:0x007f8fa9eac028 @author="Wojtek Widenka", @title="Selfish Presentation", @date=#<Date: 2016-10-26 ((2457688j,0s,0n),+0s,2299161j)>> > object.foo 4: #<Exlabs::Presentation:0x007f8fa9eac028> Exlabs::Presentation => nil
  • 4.
    SystemStackError: stack leveltoo deep def self.h @h = 'st' puts h end module Exlabs class Examples -> end end def self.a puts a end def self.b puts self.b end st => nil def self.c c = 'st' puts c end def self.d d = 'st' puts self.d end def self.e self.e = 'st' puts e end def self.f self.f = 'st' puts self.f end def self.g @g = 'st' puts @g end > Exlabs::Examples.a NoMethodError: undefined method `e=' for Exlabs::Examples:Class def self.i @i = 'st' puts self.i end
  • 5.
    SystemStackError: stack leveltoo deep def q @q = 'st' puts q end module Exlabs class Examples -> end end def j puts j end def k puts self.k end st => nil def l l = 'st' puts l end def m m = 'st' puts self.m end def n self.n = 'st' puts n end def o self.o = 'st' puts self.o end def p @p = 'st' puts @p end > Exlabs::Examples.new.j NoMethodError: undefined method `n=' for #<Exlabs::Examples:0x007f8130af82d8> def r @r = 'st' puts self.r end
  • 6.
    SystemStackError: stack leveltoo deep def self.h @h = 'st' puts h end module Exlabs class Examples -> end end def self.a puts a end def self.b puts self.b end st => nil def self.c c = 'st' puts c end def self.d d = 'st' puts self.d end def self.e self.e = 'st' puts e end def self.f self.f = 'st' puts self.f end def self.g @g = 'st' puts @g end > Exlabs::Examples.a NoMethodError: undefined method `e=' for Exlabs::Examples:Class def self.i @i = 'st' puts self.i end
  • 7.
    SystemStackError: stack leveltoo deep def q @q = 'st' puts q end module Exlabs class Examples -> end end def j puts j end def k puts self.k end st => nil def l l = 'st' puts l end def m m = 'st' puts self.m end def n self.n = 'st' puts n end def o self.o = 'st' puts self.o end def p @p = 'st' puts @p end > Exlabs::Examples.new.j NoMethodError: undefined method `n=' for #<Exlabs::Examples:0x007f8130af82d8> def r @r = 'st' puts self.r end
  • 8.
    module Exlabs class Presentation attr_reader:author, :title, :date puts "1: #{self}t#{self.class}" def self.ish puts "2: #{self}t#{self.class}" new(author: 'Wojtek Widenka', title: 'Selfish Presentation') end class << self puts "5: #{self}t#{self.class}" def bar puts "6: #{self}t#{self.class}" end def self.foobar puts "7: #{self}t#{self.class}" end end def initialize(params = {}) @author = params.fetch(:author, 'unknown') @title = params.fetch(:title, 'unknown') @date = params.fetch(:date, Date.today) puts "3: #{self}t#{self.class}" end def foo puts "4: #{self}t#{self.class}" end end end # enter the text from left side 1: Exlabs::Presentation Class 5: #<Class:Exlabs::Presentation> Class => :foo > object = Exlabs::Presentation.ish 2: Exlabs::PresentationClass 3: #<Exlabs::Presentation:0x007fce5514b218> Exlabs::Presentation => #<Exlabs::Presentation:0x007fce5514b218 @author=[…]> > object.foo 4: #<Exlabs::Presentation:0x007fce5514b218> Exlabs::Presentation => nil > object.bar NoMethodError: undefined method `bar' for #<Exlabs::Presentation:0x007fce5514b218> > Exlabs::Presentation.bar 6: Exlabs::Presentation Class => nil > object.foobar NoMethodError: undefined method `foobar' for #<Exlabs::Presentation:0x007fce5514b218> > Exlabs::Presentation.foobar NoMethodError: undefined method `foobar' for Exlabs::Presentation:Class > Exlabs::Presentation.singleton_class.foobar 7: #<Class:Exlabs::Presentation> Class => nil
  • 9.
    module Exlabs class Presentation attr_reader:author, :title, :date puts "1: #{self}t#{self.class}" def self.ish puts "2: #{self}t#{self.class}" new(author: 'Wojtek Widenka', title: 'Selfish Presentation') end class << self puts "5: #{self}t#{self.class}" def bar puts "6: #{self}t#{self.class}" end def self.foobar puts "7: #{self}t#{self.class}" end end def initialize(params = {}) @author = params.fetch(:author, 'unknown') @title = params.fetch(:title, 'unknown') @date = params.fetch(:date, Date.today) puts "3: #{self}t#{self.class}" end def foo puts "4: #{self}t#{self.class}" end end end module Exlabs class Stranger class << self puts "8: #{self}t#{self.class}" def bar puts "9: #{self}t#{self.class}" end private def private_method puts 'Just a text' end end class << Presentation puts "10: #{self}t#{self.class}" def bar puts "11: #{self}t#{self.class}" end end end end # enter the text from left side 8: #<Class:Exlabs::Stranger> Class 10: #<Class:Exlabs::Presentation> Class => :bar > Exlabs::Stranger.bar 9: Exlabs::Stranger Class => nil > Exlabs::Stranger.private_method NoMethodError: private method `private_method' called for Exlabs::Stranger:Class > Exlabs::Presentation.bar 11: Exlabs::Presentation Class => nil
  • 10.
    str1 = 'String1' str2 = 'String 2' class << String def foo puts 'foo called' puts self end end class << str1 def bar puts 'bar called' puts self end end > str1.foo NoMethodError: undefined method `foo' for "String 1":String > String.foo foo called String => nil > str1.bar bar called String 1 => nil > str2.bar NoMethodError: undefined method `bar' for "String 2":String > String.singleton_class.method_defined? :bar => false > str1.singleton_class.method_defined? :bar => true > str2.singleton_class.method_defined? :bar => false
  • 11.
    str1 = 'String1' str2 = 'String 2' class << String def foo puts 'foo called' puts self end end class << str1 def bar puts 'bar called' puts self end end > str1.foo NoMethodError: undefined method `foo' for "String 1":String > String.foo foo called String => nil > str1.bar bar called String 1 => nil > str2.bar NoMethodError: undefined method `bar' for "String 2":String > String.singleton_class.method_defined? :bar => false > str1.singleton_class.method_defined? :bar => true > str2.singleton_class.method_defined? :bar => false
  • 12.
    ▸ ruby istricky ▸ in ruby everything is an object ▸ from ruby implementation point of view Class is slightly different than Object struct RObject { struct RBasic basic; union { struct { uint32_t numiv; VALUE *ivptr; void *iv_index_tbl; } heap; VALUE ary[ROBJECT_EMBED_LEN_MAX]; } as; }; struct RBasic { VALUE flags; const VALUE klass; } struct RClass { struct RBasic basic; VALUE super; rb_classext_t *ptr; struct rb_id_table *m_tbl; }; ▸ classes contains methods while objects variables (this is confusing, not sure if can explain)
  • 13.
    ▸ when weimplementing new class class NewClass def new_method end end ▸ we are creating new instance of Class (we can use instead Class.new syntax) ▸ There is also constructed singleton class (eigenclass, metaclass) in which methods are stored ▸ Since it is singleton it cannot be duplicated and is connected always with one object ▸ ruby is tricky, metaclass is transparent, so calling #class on object will ommit it
  • 16.
    "Nathan".is_a?(BasicObject) # =>true "Nathan" is an object. "Nathan".class #=> String "Nathan" is an instance of the String class. "Nathan".is_a?(Class) #=> false "Nathan" is not a class. "Nathan".superclass # NoMethodError "Nathan" has no superclass, because "Nathan" is not a class. String.is_a?(BasicObject) #=> true String is an object. String.class #=> Class String is an instance of the Class class. String.superclass #=> Object String's superclass is Object. Object.is_a?(BasicObject) #=> true Object is an object. Object.class #=> Class Object is an instance of the Class class. Object.superclass #=> BasicObject Object's superclass is BasicObject. BasicObject.is_a?(BasicObject) #=> true BasicObject is an object. BasicObject.class #=> Class BasicObject is an instance of the Class class BasicObject.superclass #=> nil BasicObject's superclass is nil. nil.is_a?(BasicObject) #=> true nil is an object. nil.class #=> NilClass nil is an instance of the NilClass class nil.superclass # NoMethodError nil has no superclass, because it is not a class.
  • 17.
    Class.is_a?(BasicObject) #=> trueClass is an object. Class.class #=> Class Class is an instance of the Class class. # this is probably the most important part. Class.superclass #=> Module Class's superclass is Module # 2nd most important part Module.is_a?(BasicObject) #=> true Module is an object Module.class #=> Class Module is an instance of the Class class. # 3rd Module.superclass #=> Object Module's superclass is Object # 4th Object.is_a?(BasicObject) #=> true Object is an object. Object.class #=> Class Object is an instance of the Class class. Object.superclass #=> BasicObject Object's superclass is BasicObject. BasicObject.is_a?(BasicObject) #=> true BasicObject is an object. BasicObject.class #=> Class BasicObject is an instance of the Class class BasicObject.superclass #=> nil BasicObject's superclass is nil. nil.is_a?(BasicObject) #=> true nil is an object. nil.class #=> NilClass nil is an instance of the NilClass class nil.superclass # NoMethodError nil has no superclass, because it is not a class.