Tuesday, October 04, 2016

Scotland on Rails conference 2009 - The Ruby Object Model by Dave Thomas

of this morning what I want to do
is to talk about something which Republican come away and saying why do
people talking about that
it was so obvious you know arm
but the smallest things that at least to me what obvious for a long long time
wanna talk about is how
Ruby does objects and
the resort talk about that is I'm now talked about Ruby for
on another eight years and the WAM confusion I always see
is when people start doing that a programming and they start looking
various called how does this work has just like this no
and this clearly no Wong mental model that people have about what's going on
underneath the covers how does Ruby actually do this
and that's what I want to talk about because I hope the end of this
you have a lot clearer understanding of how will be
does ok building is that
arm this is not just be spouting I would really love with people so like
you know say hey Dave how about this or that's rubbish
about this instead right so let's let's make this interactive if we can
amenable to see you the lights are gonna Breitbart
you know process now I'll find you so let's start off by talking about
why we care about objects where two objects come from
right why are they important a pop quiz who are these two gentlemen
this man
is Christian I got this man is only on DOL
I apologize want Norway what did they invent
thank you shame on the restive you because this is your history this is
your heritage
but in the early sixties these two gentlemen were asked by the Norwegian
to do simulations all enable naval shipyards naval
boats any particular things like how they were resupplied in redox because
it's really important to trial optimize the process upbringing a ship in
giving it all provisions and then sending it back out again at the time
they were working with procedural programming languages like just like
fortran COBOL and
they started coding away I guess in alcohol
and they were rapidly ran into a big problem because
they discovered that they were dealing with all these things that had behaviors
trucks ships warehouses
but they also had all this data associated with them
what's in the truck how big the whole ship is
you know what's in the warehouse always kinda stuff discovered
keeping track of all the data Associated
with all these things these behaviors was really really hard in a procedural
so hard but they couldn't actually write this simulation
but being very clever people
having a spare five years they decided
up that the best way of solving this
was to write their own programming language and then printed
similar similar was written as a simulation language
but in order to be able to write the simulations
they also invented the entire concept
object orientation because they invented the idea
up capsulatus set updater and behaviors
his a truck it can drive you can load and unload
and it has the following state has this in the back it has so much
petrol whatever it might be think or object orientation
they invented if you have not looked similar
it's well worth it it's what is it now
forty-something years old and it's still
has concepts and that that we have not yet adopted
in current programming languages there are some very
very cool concept similar so similar kinda like bold along for a bit
and during the early seventies lol interesting but whatever I'll
then to other people got involved
is this
slightly more obscure anyone
Alan K indeed this man is Alan K
this is a man called more between them
did small talk unless
Alan K in Smalltalk took the idea is a similar
and made them the core feature of his language
and Alan K actually invented the term
object or into when he create a small
and again small talk is a 30-year-old language at least
and small talk has these features that most
model one just including Ruby to some extent
does not have smokers remarkably your object-oriented language
again if you have not yet plane
small talk and your roomie programmer you over to your cell
to go and have a play downloads week whatever it might be
and play with it also if you're interested in writing
I'll good code you put yourself
to get can textbook my brain just died
Jim can't local
all talk desperate as patents thank you small talk best practice patterns
right it's written for small talk it applies equal to Ruby
a really great book on how to respect your coat keep it clean
so Alan K invents a small talk invented the term
object-oriented he was live an incredibly
disappointed like
I invented the term of Georgia not
have C pas plus in mind
I wonder why that is
show that slide quite often to I am
symposiums Java programmers and the although haha look at C pas plus it's
not like the language
I put that up
see the point is
that what's become
object-oriented programming is not the way it was conceived object orientation
was block programming
with classes think about it
if you have someone in your office who is
unfortunate enough to like UML right and you tell a pay-go design the system
what diagram to use what's the favor Tiger
use last Agra design using classes
which is insane only good diagram in your military activity diagram or as we
used to call
Petrina that's a really useful blog and pasta Graham
democrat a programmer sits down to write
Georgia program and out program nowadays what the first also very first word they
we no longer do
object orange development at least
we don't in the more conventional languages c plus plus
shop job instead what we're doing
is class order to develop and if all you do is class orange
development then two things for civil
you or not using Ruby and certainly
you're probably not this on your programs right because it probably means
you'll be doing things like inheritance
an inheritance is the work of the devil
you should not be using it ninety-nine percent the time because
inheritance leads the coupling and all the other since the fall
the nice thing is though Ruby
is an object-oriented language a true
object-oriented language if you're in the arm
charity tutorial on Thursday guess what you so that chatter Marcel
developed a program actually had
what looked like inherited and I wasn't class
insight that's because
Ruby is truly object on language and so
I think it really is important that everybody understands how to do
objects really so when I first came to ruby was
actually ten years ago and
I really got into I felt totally in love with it and I spent a long time
trying to understand how objects work a long time
I mean probably four or five years
trying to get some kind of model in my head
of what was actually going on when Ruby
was doing its object its current harassing it took that long
arm Mike Riley to get it
it was just so blindingly obvious
so utiful that is fueling your share
now wall things the I did was I went through read the source code
truck what up what was going on will be actually do this
an is helpful in a way
sorry in that
if you look at the source code you can see that worried us
objects is it emulates this idea that object
is state behavior if you look at the implementation of the single
our object you will do fine state the behavior
in Ruby we call the state instance variables
now every object
has instance variables associated with it that's where we state
and the behavior is by large
methods we also have props as well
by large the behavior associated with an object is a set of methods
it's pretty obvious you can see that really easily is pretty natural to think
that way
let me go to get deeper into it you come across
diagrams like this this is actually a diagram
in a so far object see in the resource Co
and it describes the
classes another classes that make up Ruby attempts
to make clear Howard use
that plus I'm sorry
everything is object object is a plus class itself
is an object does not make it clear
it doesn't
it's a very orkut diagram to look at because it has always wear terms yes you
read the comment that goes along with it
it talks about meta classes and met object protocols
always comes to it makes it very very hard
to see so let's try into this
a different way Astron cut back to the
the underlying basis let's start
by looking at a really simple variable in Ruby
there's a predefined read only variable
call cell all know about it
if your Java programming this it's the current object
it's the object which is I'll look top
whenever you go into an instance variable so if you take at Fred
it goes tries to find the instance variable Fred in
cell because reversal is an object objects
where we store state
we also yourself as the place where we going to look up methods
if you don't specify an explicit receiver
you say do something without him
I don't say X not do something to say do something
then ruby is gonna go and look for that do something method
in cell associated with object so
so self is phenomenally important
so fiscal like the thing that centers all Ruby code
and if you understand cell and if you can
workout what self is at any point in your running code
you basically understand Oristano
about mister programming
honestly you don't need anything more so
so the first thing to understand is
what sets cell how do you get soaked changed
because obviously the changes in Seoul is what changes
ruby's you the universe excusing
so understanding what changes so is absolutely critical as far as I can tell
there are only two things the change cell
shelf can change when you call a method
and sell changes need to find classes
and models as far as I'm aware
those are the only two times the self changes
so let's look at the first other to some light coating
ceramic all to myself the Huskies
so let's have a a variable
like variable okay so at this point the verbal animal
holds a reference to the string object
pack right nothing particularly exciting there
and I could say something like put s animal dot
up K and if I run that
I get cat is exciting
cool okay thank you very much I'll see you next year
notice by the way really want my long
runs perfect woman sex right now use Ruby 19 switch to it
art so those are capital a cap
I'll object and what's happening here
is that are invoking the method okay
on animal the Roosters okay
I'm gonna have to go to find up case method
in the object Associated with that cat
string and indeed that's exactly what it does
let's go have a look what's actually happening behind the scenes here
there's our animal variable and its referencing
some object some globe thing here
object prostate behavior so the state or object
is the string value Pat right it's a sequence of characters
CAT at this point we're no longer talking about Ruby objects were talking
about actual characters at this point CAT
we said objects also have behavior and the Ruby the behavior
said beth is now because all strings
share the same basic behavior
it would be stupid to put that behavior
in each individual string object so instead
what really does strips out and puts it into a separate bill table here
a table up the things that a string can do
and so we have basically a pointer from all
pack object table
methods and when Ruby
sees animal dot up case what it does is it goes
to our animal object goes one to the right
to find the table methods and then searches through that
until it finds the code up case on it finds that code
pretty simple right now
because we have to go out live in the real world
have to reveal ugly secret what I call
table methods their is actually
but fundamentally all a class is in Ruby
is an object that contains a table methods
its object just like my animal object is
and as the way in the same way the analog holds the character Capt
string object holds the methods up case down case
stream trip whatever trim whatever it might be
so wanna talk about table methods I'm simply talking about object
the contains as part of its state a list methods
so here is the first secret understanding
Ruby metre per going already the object model
and that is every single
method call works exactly the same
way there are no special cases
in all cases when you invoke a method
on that object ruby goes want to the right
looks for the method alright
so what happens
if I did something like I wanna know
object ID run that means that
here I'm getting was it some magic number out
object ID how does that work
does not do much my my diagram here
their is no object ID method
string so clearly something else is taking place
and that's the next pop the equation
and that is when really goes want to the right
searching for the methods if it cannot find methods
in this particular object if
follows a little chain call parent
up to another object not a class object
in source code terms the subject is the Parent PLUS
the superclass this wall so if you say to string
what's your parent with the report object
no if you are back to record model and you said what is your parent it would
active recall calling on base
so the pair is simply the superclass
up the object so a method call now
is want to the right search
you don't find it co-op search
if you don't find it know all until you hit
top and the top in a review on 8 is object
re 19 its basic object
if you hit the top and you don't find nothing you're looking for
then go back down to the bottom
and not looking for a method called method missing
and fines method missing in the world chain
you executed and back object or
in Review Online basic object implement method missing
and that's what it is reports but no error
when you talk about nothing that's not built into the interpreter
what's doing is executing code define
object okay so that's how much the calls were coming but you all knew that
there's nothing exciting they're really
up let's play around a bit let's go back to our Alexopoulos
you probably know that you can do this you can define
in methods on individual objects
there's a lot of controversy on how you spell now
that's my way alright
so I can run that and
my animal all my cat do that
but if you take another cat can say that
only one object has that okay
so far so good but remember we said
I said asserted Ruby only calls object because methods
one-way it only ever call our method
by going once the right and an OPP so
somehow we have to make that speak method
available the sky
how we gonna do that
how do we make speak method available over here
the only way that can happen
the only way that can happen is if we have something
here have Speakman the fine right
on way to work so what was really do
ruby is a dancer
a brand new thing
here this is a plus
has that method that we define and it's it's
one to the right all are animal object it does not sit once the right
any other string it simply the class
all that animal object so we define that speak method
what we did was we created this thing here
we set the class pointer of our animal to be that
thing and then we set the parent all that thing we created
to the original class the animal so think about how that works
now if I say animal not speak what happens I'm here
no one to the right I find speak excellent just what I wanted
if I said animal dot case what's gonna happen
ago one to the right I don't find it here ago op
I find what they're so it works and certainly
animal 2.2 per animal object idea what everyone cool
is gonna keep going up the class Chancellor find so that is how ruby is
gonna implement
ability support methods on
individual objects the reason I have question marks here
is that this particular class is effectively
anonymous and not only
does it not have a name it's really really hard to see it
when Matt's designed Ruby
he decided make these little
classes effectively invisible if you said
to animal what is your class it will say
it's a string it lives
it's not a string it's one of those guys ruby is not gonna tell you that
show you a bit later on how you can get to that guy the other problem is
there is no I official
name for what this thing is some people call it the singleton class
because it's the class associated with a single object
and there's only one of them for object some people call it the middle class
some people call it I can plus
I started calling ghost class
because it's got an invisible just sits in the background
whatever it is sinks in class
I and class met a class they're all the same
idea its simply a little invisible class
will be creates for you to make certain things per
it'll make sense
Matt schools and different things he tends to use singleton
arm but if you're sure either source code he also calls and virtual
so I don't know I'd I mean
will be really nice we dislike came down a mountain and said
from now on you know
but yeah unfortunately not
sorry it would that's true
so that singleton class
as I say it's got lots of different names is
just like any other class except it has a little bit
Senate says don't reveal me to the outside world
whenever you create
methods on object so say for example
I create back here I've pretty much speaks method on animal
releases oh I need to create the singleton class
there's not one there to start with by t-fal animals just a string
such class point2 points to string but the first time I do this
it said okay now I need to create a single class
so it does that effect create new all
releases oh I forgot a singleton class
so put the run method in that singleton class
the New Year Wong singleton class
for object and it's created lisa Lee the first time you need
so that's the first part
afar equation the idea
that there's are two things that change
cell and 100 she know what
skip will be here when we talk a bit about how
how our method
gets call went down here say
animal dots speak reactions to go to find that speak
right it's really useful I find
to think about the mechanism as a kind a four-step process
the first thing is
that Ruby takes the receiver this guy here
animal and sets so to be that receiver
so before anything else happens
reset cell to be
that half object and that's why
right we've got on the diagram effectively that thing on the left
though the captured
that's where self is at that point Ruby
then looks for that method in
the method table associated with object classid
object so it set so then
scams insults class for the object it doesn't find it
than does the scam missing eventually will find some
cool you invoke that matter now it invokes that mess itself has already
been set
to the receiver to Pat so
as that method executes soap is going to be set
to that help and then finally
when that method exits returns Ruby restores the original value cell
pops a pack of stack if we can show that using
classes on the
so there since this method that sets
as this method it sets an instance variable
so I say new occurs on creating a new instance that plus
I say d top wall then
what's gonna happen South get set
to the instanceof Class D it searches
in the class for the method one if finds it so did folks that method
cell is set to that instance is a Class D
so it's it's the instance variable to miss his wall
it's being set this is verbal a is associated with a particular instance a
the with an exit and so
is reset to what it was before we called the method so that if I
call to same thing happens
self get set 2d because looks for two methods it finds it
goes to look for instance variable called a and its associated with that
particular instance
for brings back the value Wong here in that movie is gonna say
wall if we change your program
and instead
just call to
here from than
the method called to has no receiver
if a method call has the receiver
than Ruby scripts two steps in the matter calling
arm chain it doesn't reset itself doesn't need to
soap stays the same the end it doesn't have to
pop back to previous value so so all it does
is the method lookup the method call so here
we sets of two bd find that matter the Wall
set ap a in that instance then we call to
we don't change so so simply look for the method to
in the Class D put this at a a reason for instance find
that puts it everything exactly has you expect that may seem like I'm laboring
the point but
whenever you get stuff better programming and you get
stop try to lookup how come that nil I
guarantee if you track value
cell you'll understand
where the sport has come from that's a really good question let's say
we always have a call yourself right self exists all the way
throughout Rico let's just that but
and I said that whenever you call
a method it's always been called on object Hills
how come that works well the reason that works
is that at the top level so has a value now
if you lost soul what is it says
I'm main but if you take a little bit deeper
and ask what's your class
it says it's an object so
at the top level a program is actually
a om instance object
with a couple weird little properties built in to make things work
okay when you're defining methods
in that the top-level you actually defining methods
I got minute officially in the sink in class object instance variables are in
object now where does put this come from
well protests is actually a
ago group right at the small
protest is actually a method
say one word for
curtain put this is actually a method all the kernel
about module and the kernel module
is actually linked into or included in class object so
any method this defined in model colonel
is actually I private matter the class object
so it's a protest just basically ghost
object and looks for put this matter in this method
find executes because everything is an object
protests could be used anywhere so I could for example
say forget really confusing
IRT it might be private I'm not too sure
we have a look
its private yes I couple in theory it's available every object because
its metal alright
so we've seen how so
changes on a method call the other place I set itself changes
is all a method definition class definition so let's have a play with
so let's define a class
yeah 10
now you know that in Ruby
class definitions are excludable code so I put one here
to their second output
123 pretty obvious the right because
the code up the class definition is executed or
it's just any code like any other with should be coverups cure to reprogram
you can do something like this now only has
13 it doesn't excuse the class definition right its
just code so
we said itself always has a value so self must have a value
inside a class definition let's have a look see the value is
and I put sell for the top
so inside and so
at the end fire on that
it says in the class definition self is set
today or what is day
let's go back and have a look I say
self-talk specter's a little
change in a long line not help alright what actually is the classic day
isn't object of class class this is where
English top
in London just like Java
or see shop whatever we need to find a class
it is a definition it's a declaration almost right we say
class something that something is somehow magical it's put into a symbol
table with classes
somewhere that's not true in Ruby in Ruby
classes are objects just like anything else so for example
here my day is simply a constant
thats referencing a class object so I can say you know
class again down here
the whole I'm just call a method on it yo
I could a sign it to a variable
I could say X equals day
and then I could say all
equals extort new nothing special about it
its just an object I can assign two variables and also its cool stuff
just runs so
when I define a class that class what's happening inside Ruby
is it creates a brand new class object and then
the signs that class object to a constant called a
inside the class definition self
is set to that class object
so far so good why this is set
so to the class object was warm
really really good reason let's say you want to find
what other languages call a class method no
well in Ruby you do that by saying
there self-taught speak whatever it might be yeah
if I see all the school Ruby programmers
using that were all cool kids now use
this their equivalent because cell is set
to the object right so
if we do that then I can't book that object by saying they've got speak
this run okay so
most folks will say that defines a class method
those books all wrong was really doesn't have class methods
there is absolutely no difference between saying
their self-taught speak and saying their
animal not speak the identical
really doesn't even know that you're doing that inside a class
nothing special about it so what happens inside really when you do that
here is
our object model but remember I said
that that class misconstruing
our animal our class has a class of its own
yeah the classes string is plus plus
so if you ever invoke a method at the top level
on a string like that sorry on a clustering what
string not new or something like that what's gonna happen is your info key on
that string object
so the same rules apply you go want to the right the top
right so whenever you do a dot new a-class
it's a regular old method call on the class object
egos want to the right find new doesn't find it goes up
object released a report I we've already seen what happens
we define a method on a specific object
in this case we're going to find a method on our class object
my example typing their it was day 100 the fundamental on the class
so it is really do it inserts from local singles in things
exactly the same as on a Sat animal doll
so now if I said day thoughts P
it goes want to the right finds it well-executed
is that cool or what
right at a stroke
scored a metal object protocol
ass stroke the meta object particle Ruby
has eliminated an entire concept the programming language
there's no such thing as static method of class methods it doesn't need them
all methods are the same I think that is just gorgeous
it's really really really
you as a concept all you need to remember
in order to met a pretty roomy is that
this is very walls on the console and Methods
a look top insult class want to the right
and then aw remember that you know
Oristano a better program now I live in
in Dallas and acolytes is weird
transatlantic Sargasso Sea accent um
but we have a whole bunch infomercial on TV
in in States and invariably
it's obnoxious a welshman do them and
you know the cut the catch phrases but wait there's more and there is always
Ruby then goes on and gives you some convenient shortcuts for getting stuff
but the shortcuts on not necessary
there's no need for them in the language what you've seen so far
is good enough but we take it one step further
as well as being able to
define just method or object I could say at all
equals at know you see me before I typed
animal dots P or whatever that was
I that's one way of defining a method
on the object animal cat another way of doing that
is using this notation
right class less than less than animal
Iran that help actually called in
so what happens when you say class less than less than
something the Western less than notation
basically means open-top
the singleton class this object stop putting methods
into that singleton class it's a kind of a shame
the arrows go that way if there is one
this way instead you be more obvious what you doing in the diagram
you're moving to the right yeah
your defining methods want to the right where you would normally define methods
and that's all it takes place when you say less than less than
if there's no singleton class for the subject recreates 1
and then just dump methods into that singleton class such as what diagram
here's our cap object
here is our singleton class or
wanna call it dictates pigment is a string object when you say
class less than less than animal it says if you have the ability to say
class and then whatever the name is this guy is yeah
all you're doing is opening up that box sick don't methods into it
yeah why would you want to do that
well on pets
on individual objects like that it's not so useful it can be occasions was not so
I personally think def animal got something is clearer
think last less than most notably methods into it but
you may have seen people coding
a pic if you read the real source code they do this all over the place
you may have seen people doing this we should make DEP speak a macro
ownership and type thank you for telling me by the way
right see you may have seen people do this
and quite often they'll put the comment above it
to find some
plus methods
this is your
cargo cult thing ninety percent the people
could do that have no idea why it works they just know that if you put classless
the most myself
then ruby must be looking at that going cool he want
class methods up hope there's no special case
what so ever this is exactly the same as saying
class less and less than animal what are we doing
we're opening up the middle class singleton class dumping all methods into
so in the same way saying they've
does our death so not speak here puts that speak method
the same in class just approximately plus a foot speak method into it
now why do I say it's more useful in class is the
is with individual objects 10 things that I like to do
with class listen cell is to include stuff
at the class level that's normally done
at the object level put
people who are new to Ruby often make the mistake
some type so bad
delete move what code rings people who are new to Ruby
of a big mistake up defining their instance variables
in the same with you do in Java no job you'd say something like
you know static string whatever might be up
in your class definition what people think I have to do the same
in Ruby that's all the fun missus variable like this
and then they'll be really surprised
investors in Java right so we'll say
d equals they new
house AP ok for a
alright so do something like this not be really surprised
there's a bug in Ruby because I set my instance variable
but has not been accessed in the method but now you know why
yup because what's happening
is that
instance variables are always store relative soul
what's soulful here the class object
what's so former ex in this method it's an
instance the class it's a totally different object so there's absolutely
high in between this and this but
if we can have a method executes Wentzell
is set to the class object then we can get to the instance variable
so if I was to say self-taught get
fret their and here rather saying
day talk the deal get rid calling on the class
then that works right because
the class for both reasons we will set the same art the object is set to the
same thing
the reason I said that class lesson this itself is useful
here so I can do this
yeah so what I've done
after accessor only excuse on instances of
a shifting to the right I've made it now available at the class level
so now I can say they've taught
pretty cool wilma
with this Alfred
group up too late too late
that's pretty cool isn't it your heart crap
I think that's pretty cool
yeah everything you do at the class level
%uh the other uses little you can make your class the whole shifting across
so that's a useful thing to be able to do
and that's just an example
the other thing you can do it again this is just icing on the cake
you can inherit expression I
again in the church tutorial E the a muscle give an example doing that
when you say class Hussey class a
and last be
less than and
alright say look at that and every says oh yeah be subclasses
a right if we ignore the fact that subclasses the work of the devil
then you know you look at that you think okay that's intact
so what Ruby must be doing is here
that labor plus and that means subclasses
and that's lame a plus by the way the reason we use the symbol
subclassing look like two horns on the side so
you think Ruby must be looking at that and say nothing of the class
no it's not is really doesn't know about classes
real no idea about classes or does not object
member we said a is a constant
schools the I'll for subject so what ruby is actually looking at here
is not what they have a class is looking at an expression and expression happens
to return
a class object so if up here I said
weevil equals a now work perfectly fine
it's just an expression so I say
protest there as the parents
source a plus right
now that plus object string given a name
a which is why reports a but it's just an expression
that means you can do some fun things
I could do something like class
silly is a subclass all
if Rand is less than .5 a
array otherwise hash
that is why I called up typing so then I often say
PE silly new
saw a a superclass
run that code
ok it's alright sir shoes
i doing wrong
out is ideal just do this silly
new area alright
so that's how to create a hash ash again
array that time hash that time yeah
if you got callings that you really don't like
but there is
one really good use %uh this ruby has this really incredibly useful and much
underused thing called struck now what struck does
is it constructs classes so I can say
I say person a cost structure not new
name age
10 I so what that does
is actually on the fly construct a new
close object by saying right
it's a class and then I can create a new person
I could say day equals person
or new a and see I'm
27 alright
so just construct a new object that class but
the structure itself is simply a day to hold quite often you'd like to decorate
that with different methods to do cool things
arm and ask all look a bit tricky you could for example say something like def
person dot 2s
and they could have something like see
so name you
are and I said
protest if they're sir
up on the wrong thing I'm sorry yep
it you're right
okay I need to put that okay I put a spell on methadone
Dave just for now ago
I so I wanna put that method all the people like create
so what I could do is re
open that class and define methods on
so I could do something like class
person except now to make up okay so much and then put my 2s method
in there do that
or more conveniently
I think what I can do
is rely on the fact that I can subclass from an expression
as long as an expression returns a class object and this case
a subclass from that
construct a new class based on my personal check
but now house that method not just runs
nothing you can't do without the other techniques for
but these ability to extend the class and the ability to suppress from a class
are really quite fun just quickly
sir that is syntax unfortunately you can't override that operator
inheritance itself is not know if that's ok the question was is
this less than some kinda method that's called on the class object
that would be so nice walls but it's not
yeah be really caucusing she override it hurts
your wall
where are we
okay just real quick licks and I would think a couple on here
just one last quick thing including extend because again people get a little
confused about
what include an extent you have module
occurring at times if I include that module in the class
then I can
the instance methods that model alright
and so this account like mystique hilltop
particularly because in languages like C++ see shop Super Sport
include is a textual substitution it doesn't find something
and dragged in so quite often people
explain include by saying it takes the methods is that
and helps them to that class but it doesn't
and you can tell it doesn't because
if I was the fine
that meant that there right gets invoked
if I was to find it before
I do they include it still gets
yeah so the clothing has to be some were separate from Mike wells
and what's more it cannot be
might direct class because we know that that no
is in class today so how do we want to the right to get my methods
we know it has to be there so include hospital with funky
building about include is that if I create an object here
than ok a lot more joe M again and change it
even though I've already created my deal object here
speak here if not already created my
the object change in the module change the behavior
that the object something funky must be going on what's actually happening
is that his mind
mike d class MyClass whatever it might be when ice
use include in it Ruby inserts
what all those little weird funky classes single glossary like
has the immediate parent of my class
and its methods
all the methods that module I have to draw them
out here because this is separate to the module it does not make the module
my parents all the classes parent and it can't make much mccur
because I can include a model in multiple classes and so
the parent link has to be different for each other's so how was it really
anonymous class like that and then using
the magic pointers makes the methods of this guy
the same as a method to my module if I then define
a new class and include that module in that class
then a new anonymous class gets created
its methods also sure that models but the table
that's why if I subsequently change the modern methods and that module
they get changed every single instances that class
but again there is no special case here
a method call is still want to the right
and then opt so
it includes them
I bases stuck in the mall so you aren't you got a new
a new one of these guys for each module includes right
the medical ask those at the bottom
your class and the models going up and effect it's actually
that's all those things where I can say that glibly it's actually worth playing
with that
and can play with it but discreet in the same name method
each wall and then comment in announcing how it works out
you find it works out singleton class
model that's a plus alright
so fundamentally this is the picture I would like to have in your head
what it looks like inside the room Ruby programmers runs we have objects
all these are objects ross's everything a real object
right all these are objects some objects whole
state used your program Pat so whatever that may be some object
all things are classes but again that no different to any other object
just have state wouldn't sure classes case
said methods have on some
about behavior continues are they have behavior
have apparently but that's your fundamental
object model all you have to do
to get better programming is understand
house elf works understand how selfish changed other biometric
up by another call or by class definition understand
the instance variables are always store in cell
and referenced in cell a spot wall
part 2 understand
the all method calls in Ruby are identical
there are no special cases every method cool
is right and then all you're inside a class object
right and then all and thats
all there is to it sense
cougar for the matter program
enjoy world

No comments:

Post a Comment