Thursday, September 29, 2016

Ruby Conference 2007 Advanced Ruby Class Design by Jim Weirich

I work for edge case which is a Rails development company have been working on
for about six months before the app
I was are pretty involved in the Java world as well doing Ruby
are and Java kinda at the same time so that's kinda my back row talk a little
bit about that but first I wanna
mention the word advanced here you might work think what he's really
going to go deep into some really complicated
are Ruby code here and that's really not the case what I want to talk about is
not so much the advanced Park
with the roomie part about iPhone
that arm language
particularly programming languages him to shove
shaping for the way you think so let me give you a little bit of my background
so you understand where I am coming from I the very first programming language
ever learn
was four crayon and that was in my physics class and work but not for
cramped drive some Calcot waters
also to plot the data from our physics lab experiments
that was great a really loved it it turned out of
I've kind of all the whole computer science thing rather than the physics
things after I got outta college
our first few jobs involve courtroom from their idea doesn't see programming
got very involved in model 2 does anybody remember Mar 02
at the time
it was up I was there beautiful language
it have a really great concept in the things I was worried about when I'm
was bottler 30 cohesion and coupling am also too I felt got a lot of those
things
exactly right from that point then I started getting involved in
object-oriented languages
which took the ideas of coupling and cohesion and modularity
even farther so C pas pas was my first entry and object-oriented design
up from there I got in the eiffel eiffel party but contractor
out to right reliable software rate lined with beautiful
in its own way beautiful in a very different way the ruby is
still a beautiful language in its own right I think a also
involved in the Java law alongside about the second programming language ever
learned was worth
yes these are your father's parentheses
and is quite by accident see I signed up
when I discovered hey this programming thing is kinda fun in my junior year in
college I signed up for
introduction before current programming so I could learn four credits will but
better
I got a class and the instructor
started drawing core code on the blackboard we didn't even have white
works backgrounds
those are all it is OK star writing code on the black poor
I know something right away it had way too many purposes to be four per
i sat in that class for three days
totally confused I had no idea what he was doing
why all those processes are you a little better for Priya
I knew this wasn't right over certain it dawned on me
what he was doing with the print the season because the recursion
the whole thing it turns out this
introduction introduction to 4 per course had Daniel Friedman
as the instructor Daniel Friedman those are you know north
the author of the littlest for the little schemer all that you've just come
out with a little less per book
at that time so we were getting a lot of good little whisper
are techniques thrown at us
in class and we learned with sparboe first reporters that the courts and then
we switched over 24 cramped so we did get our introduction
before print eventually but by then I was hooked so I learned left
after our college arm I got the ball before 4th was great because it's a
its interactive language but its
design first systems will release more resources and I didn't have a lot of
money just got a college
I don't have enough money to buy the ER it was either in Apple to
we or Apple to RR trs-80 those with the big computers back
I'm I don't even have enough money for them right out of college so I bought a
little
single board computer with UK if memory of
you know what fourth runs great to cave memory I had a
editor an assembler compiler and enough for my application code all in 2k a
memory which is really cool
fourth was very attractive not dynamically typed in any way you like
ruby is but it was interactive in the same way that Ruby in the same way that
with spears in a lot of ways
up from there I i kinda graduated up typical
a wall fickle pickle
through program I was very few it's been awhile since I've done that pickle was
also cool in a lot of ways
finally landed in the Pearl as my standard language for developing pools
art to help my programming up C pas
awesome job so paroles kinda like to language our job or
and C pas us we're kinda by professional languages that are used to get recoup
real or doc right are about that time was really looking for an alternative
pearl cause I really like for like
really I really love the work done broke but you know walk
it was unreadable and when you grew your Perl code to insert size
it just in scale up a larger problem in my view
ike I could have a pearl a full article rates
so I was looking for alternatives in about that time Dave Thomas
I think it was the extreme programming mailing a mention about this will like
which Paul Rubens so I grew up
Ruby download it compiled on my machine
tried it and in one day I had completely converted over
using Ruby rover for also that was by a doctor and what was great about Ruby
it wasn't hardship Korea did scripting language
and I was already really involved in the whole movement
and are working with that from the Seans
a C pas plus the Java were all the eiffel world
but I want to point out that my or experience up that Prime
is entirely what we call static languages
so my view always very much shape by
the languages that I used to do or not I'm your own little bit a small are you
us no small part that I could reach more 0 red never written any overt
anger so the concept smallpox really work
apart yet that part ruby was her opening things to do in Ruby
I as opposed to like a static language like Java was
was incredible I really love the whole movie experience
there's a saying you can write 4 p.m. coded any language
perhaps the updated version A back should be great Java code in any
language
I think a lot of how many people have come
to Ruby from drop there's a lot of us here
I think when you if ruby is your first introduction to a dynamic 00 language
you're going to bring concepts from Java and C pas
of yep are along a concert for poor or gonna be great and the good work the
whole thing about modular view
up like that's going to come right with that's going to work well but there are
weeks that Ruby that are in
see vocal in Java code
seaport we're going to talk about some of those
techniques that's what I really want to do is if you are boxed
by the language if you think you know I really want to get you thinking
outside about of so
what are you going to expect from the stop
a first-ball I have three examples more or less reliant
code that i'm gonna show you right these are examples library
I've written using the production and some there's one example one that's
just kinda cool idea the reason I'm into that because these examples half before
you have to be interesting and they have to I have to be excited about talking
about
I will make a confession I was working on this presentation
I got to the third example
I was writing up and I was about halfway done
riding up I made this discovery
or I didn't like the exam what was interesting quote
but the process of explaining it wasn't very interesting at all
me so our so I was trying to decide whether
use examples for something else arm
happen you forget save my presentation file sober
few days later when I came back to revisit that the whole that's life
examples both good that's a sign from God
we're changing our example soon be abstract might've read the program was
little bit different but what we're doing here
that's okay the other thing I want to illustrate
art techniques but you're not gonna fine your standard Java
lapses C pas of programs things that
not literally run across performances vans Ruby paw
okay so let's go ahead and move on to the examples
the first one i wanna talk box number one but you wanna jump out
I call this the master up the skies in this is an example that actually
problems
from rake sure most viewer perhaps were to break
I'll bill tool for real thank you
he
summer so i think is the new piece of code UT is the fact that people
user find it useful II that really gives me a thrill more than anything else
people point useful thank you for that this is a actually a very small piece a
break but i wanna talk about it
in rape rick is all about building things and manipulating with the file so
we have a special class
rake campbell's the are
poll watch: in a file named in finding files in your project and
managing up has called the file was stopped use it a lot like this is you
see on the screen
arm you give a claw
a pattern and it will actor as an array
up those filing alike an array
almost there about for differences between
file with them real right number one it to dislike by Paul because
knowledgeable pop knowledgeable about the files so confined those Feilding
uses you can include file exclude files
Dubai patterns an awkward in traffic picked was very knowledgeable about
also specialized to ass this is the big reason
I wrote a specialized of him out because you 2s array
all things getting there your jam together
about space I really wanted it was the bombing speed
what's up file list as a specialized
to us also has a extra that don't exist
a normal right you can manipulate extension although
file believes in the file list you can come in picked up after
other interesting things or file what's that are not available
right finally are
firewood for please the reason is if you have a rate filing have a bunch of
different past
finding your rate filed like the final all
on up filelist up front be uni
operate file that won't be you
but you're justifying just in case you might Mike used by one the fact that
might be
all later so if you initialize
files by lobbying to your file directories a
searching for files building up with the file this could be quite expensive
so great finalists are lazy in the people actually go out and look
filesystem until the first well I'm you need
use the values while at that point would go out
we get the belt bring so if you define
him twenty finalists getting your rate
while not OVA after you anything I'm consuming
a reaction like music that's a would feature a pilot
actually the future were on separate on today
Mike first cut right filelist
looked a lot like yes I so well a finalist is a right
and so on just inherit from a rate and get all the Arabian your
automatically if you come from a static
language like Java or C post loss one of the first rules that you learn in class
design is
do not Eric from concrete piles that carries a whole bunch of baggage with
truth is
that in Ruby the reasons there are reasons for not inherited from concrete
playoff
appeared Priory differ the reasons
to refine job forty walk I will get to what these reasons are so i thought okay
let's let's go ahead and her from Spring Creek last month to see what happens
but actually worked out quite well begin with initialize for file most
such with this is a simple fiber superpipe
Road of your real filelist move or copy that
us but we initialize a finalist by loading the pattern restate the power
we set a resolve flag
of as we have not yet gone out top
the file link that exist files yup also be called Super
that's just good practice with your hair classic visualizer
make sure you love me initializes your
parent classes as well saw
lazy loading sup just remember the pattern
for use later Sep resolve fall so that you
know you have resolve file yup then you have to try to resolve
up and it's pretty straightforward when you call resolve you want to make sure
your revamp the
go through %uh the filesystem get all the patterns
and insert them into
self which is an array and that's not resolved through three hundred-ton
your file is now complete solving file names of a filesystem
and were resolved so the next time you won't have to call resolving
chop-chop later now does this work
one no
up I'll
a getaway we haven't resolved
yet so when we create a file of
and we try to access the first member because we have not resolved
anything yup that arabia's empty so this will pay off
what we have to do actually explicitly resolve
pilots for how painful
I don't wanna you that so way to fix their
is top automatically resolve the file list
the first time you need so we will take indexing operation
and we will replace a res version
with the version that polls with all if we have it was all
Yap and then invokes the Super for
our index what's called you're a pro actual for
indexing from now that's okay for
indexing remember there's a lot up other rate rapid
well so you gotta walk rinse repeat yes
over and over again now you follow
I would strongly recommend at this point just getting a little better programming
make this easier for you to find disposable plates will do that in a
little bit
relieve that as it is for right now okay
im are resolved great after work pretty good
I was really happy to come over rate work exactly like
us am had very few problems them also someone so
reported a BOP this code works
this is the using the plus
operator that
takes to arrays patents the on
together losses about that on a finalist
I was all filers in fact that make the
that guy called me super that would that makes the contents and the result is a
list up
everything the vilest Haas everything and you're right argument we
bus operator so that work but what happens
if I changed or we've got a small problem
array doesn't know
anything about resolving file so in this case the plots
is the operator only regular array regular array takes argument that the
pilots
looks at its sooo
I have a right and it got this far was Eric from a rape
it is a right so itself up let's compare
content well we have no content so the result is just a perforating
only memories all the cockpit the pilots which is about
that's a problem are we going to do about
back pop/rock whites
over that okay
if only up
if only there was a way for arbiter
up indicate the Ruby system all
tree me as if I were to write
wouldn't that be a nice help was then I could put that
into Wild West Ruby would workout
who using some
your or ship
be have the technology turns out there's a method called
who array to kary
that is designed to indicate exactly this behavior it says
I am an awful thank you it says
I am in object and I want to be treated
as if I were an array Ruby senses you happy Mary
effort on your part of it will call that method
use the result method should return to Rees array of saw
will use that result in all its operations or Treat up
as if it were am i right well that's fine
except filelist who
cleverly mimics about effect it
is generating Review looks the pilots is ok
your honor right I don't need the call to hairy on you
I'm just going to use your comp so the fact I can error
from Iranian filelist was what was going so to make the two
a rematch at work I heard modified filelist
not inherit from array after all
step it's a regular object
inheriting from just your basic laptop again the initializes much the same
except now
we looking through actual array object we campaign
inside the file Ste the at items
I love their we have to change or
resolving code arm
so that used to look like this we know
delegate items it's not a cone super
and now everything
is cool we could pay
an array in a fire last that make them releases a walk
I've got object not an array can you after the raid
policy I have a where we are
area on me call that will be happy Ruby does up
we get the result about so that's cool
remember this are we need
our resolve although server go ahead show you what that would look like with
dry out that code that up
implementing all those methods all over again you do something like this
free to list up methods you need to you are resolved
do a little better program music class about
the fight over also pick up actually quite easy would hurt from array
are not appear Arabia
the my all arrays behavior in fact you can
serve explicitly setting up a result pretty much be
is that explicitly listing those even after going asking array of
what are your stats about that and then iterate have just our resolve all those
a little better programming the class program right
exactly quite small so that's what we're doing today were filelist
things to remember from the house okay
some part when you want to be something else is better not true
error but to use the built-in Ruby
up to Harry the case races also
you something similar for spring is a few STR about that but that's something
very
work without so used those rather than arabs
when you want to mimic of okay box number two
doing nothing people have used
builder really don't ask
all what makes me feel good group of more
grover what for library to write
I wrote it just for the pure joy overriding
I saw an example actually room the results include a group that inspired
yes and I we could Ruby quite easily for not be quite
however in the writing abyss a productive so
little or repeat that we had to deal with one another
hawk about those corner cases right now
so if you're not familiar with builder very simple
a builder is a way of building XML very easily
Ruby created over object you then
sir messages such as steam such has made such as phone number
the builder of the best them in blocks appropriately
and builder figures out how to translate all that good stop
it XML using the names of the methods
uses the pact in XML using your block structure to figure out how to best
properly this is a simple example
abusing builder and the names of the pads
and the names of the messages matchup
who the magic about the missing I didn't write builder don't anything about
student
about names about to phone number it's just that we track
those Rapids in a builder but nothing missing
am pick the name and Greek exit million so you can send
anything acting builder and recognizes
really cool I will point after easier right XML with your
that is right up to the love that's about and it's great especially for
dynamically generated up
great rooms generate a list very usually
one stop auto database India builders a really and the
over library use what happens though
this example we have name phone number write a review
all or builder class are to our builder
object Matt Squire 2002 rock
work or walk why I asked us to pay
and he's taking a trip to Ruby obviously
a various defiance he
on what happens when we execute this little piece of double coat right here
yep clients is a predefined ruby has a method called
laugh but every object class refers
possibly up is a number of
wanna number of predefined Rapids you know obvious classes I
the there's to you ask room are
you know the list goes on overwrought use any
these predefined methods in your at some now
builder has a problem with that because it says of predefined doesn't get into
the whole method
missing thing at all and we've got problems
saw wes is pretty far I'm
fix for this would be nice
separate would be nice yeah how can we inherit from
up well actually inherently from of
we would like to be an object but not yet all be
artistic garbage for all the over
extra of comes up up we don't want to
for our overclass so horrible that
well %um builder we're going to change it so instead of returning from a
Pakistani
ever from something hope white slate
whites white I want to talk about
the day
ago
want sleepers out Yuli easy
Iraq
well
almost what happened run you see what I'm doing here
I'm Arabic all the instance methods that are available in the blank slate
Playhouse
I'm iterated you took them I'm on the following that method when you
undefined the map that make it disappear
from that class white slaying will not help
class that will not have I the will not help
house find so we just generate whoever
and remove them what happens when I run this devil forget the right that's a
bell but we also get a little
cup award because we're a little all results removing
about this a couple methods such as underscore item and underscores them
that the system there if you remove them it may
cause serious problems bus these effects
so we'll just under for unless the baby give
I'll actually I think the real for
up like slate we also avoid removing instance about was productive the
balance up
interested that they have a row and
probably not yet a while well
act that was kinda safer to stop
that's pretty cool everyone just we get exactly the right pro
great I love that simple
iraq you rock group
this good enough have recovered all the corner case
but yeah likes like was released pretty much
like that showed the while people began
writing everything papers there's a problem with this October
that these rapids are not appear in my apt to melt even though they show up
go
what are we doing here we require blinks likes a blank slate
lows it removes all methods from internally so now it is a blank slate
and then in are application we decide
wouldn't it be nice to have a globally available method called
Bay we put into kernel her was
model good art anything you define
colonel is available every OPP
what flight now if we could arrange for QoS a blank slate as always the very
last thing
overloaded loaded with at this in the kernel
for loaded like slight likely would handle remove it when it was loaded
unfortunately
it's too late with one wants leaders already loaded
already removed all those methods an hour ago having something to her or
one slick picked up automatically that was not a good day
so get this will result in the wrong number of arguments there
for the same reason to class but does not need is predefined sewer
you with this turns out this is also pretty
you with Ruby are
I'm going to refract wants like just a little bit I'm definitely is a private
method
I wanted evil caught somewhere else or preparation for that we're going
rapid optimum app hi well-used
at the High have gotten I'm the pines all our
methods and no height is available globally
and then we're going to at something to people abuse
about that at added yes itself
hook if Ruby Road saw Ruby
were it the farms method classes models
will work for a map it a
class call method
out and if it's find it will call out
and parents in the name of the map
that was just at artful so
arm I wanna rector arms to
third Lord in method at says whites Lee
Barack when we are about that I want to hide
inside a blank slate if bottles being
added with self you score so if colonel get some
method added to yet remove it from
lights lights or lights like it's probably notification have any methods
yet if you
model we're adding about the two is black pearl that's okay
what purposes only /url it's going to bother up looks like so I
a check to make sure we're only idea if its kernel
the first and last one about the data
is just a way possible health by
up into about that added that we want to make sure that you
all their old as well otherwise we'll liberate their youth
without so that that's good if I think rails has a way of doing that
on efforts up me alias up the check the real
reels has a little way of doing that very hour about such what
year is just being courteous using up
ape-like up please use so pick somewhere yes
saw I'll note at the bottom of the screen says
we need something similar for op read is
who in object because you can at beth is
replica object as well as also up
very several Todd all back
now are we good
how would cover all the qualities I added after
builder release the gap I thought revolt every
everything were who'll can also book
report you get I'm using this method
and its still not showing up builder like
still not forgotten by Axwell referee Arthur said your call because I got
everything covered against up
of a plea we could be missing anything so he sent me the code I looked over
discovered this
very similar the probably
for slightly different in
this case your create new model all capital main
inside model name we define about that
call name and then we model
a the optic class this is actually a very common way of extending core
classes
all the extension any model that pain
extensions event in one place the pork of just before
what's does actually good practice recommended our problems
it probably bypasses
about that having hook because not bad having
rapid are were moving are
it Harbaugh bowl wats up that saw
fortunately for us okay appeals there
fortunately for us there's a
hook feature call and feature another
gonna walk this code as a solving that the data
almost exactly like that we have to make sure
whenever we Blue Bottle ok apparently just is called whenever a broad
so we could fine those events we check to see
are that were worthy of it by Afghan we remove all the methods go ahead
i'd better that model it looks like so
white like ap his wallet that has turned out to be pretty good that's what we're
using
currently in over I don't think I've got any reports from many walks like they're
having problems
all look her we r knows where you have good putt putt
but if you want to see the exact details like for you to go grab hold for builder
prove that it was a really fun library to write really applaud
back
this is the example but not every library
I think the techniques your interested I'm really hoping someone will find a
good use for example probably not
about being a good use or arms about
the technique is fun that's good enough for me I like pop Funko
use enough
right israel's boat you done any real programming all
you've seen this all the time user is a reels
rather record artful
R O and has a fine about the mall
rethink fine all users me
is chip and you see
am if you want to be a say that's interesting
we have made it was perfect Markham quotes
that is actually sequel for mixing
little bit asleep code with the little bit Aruba cold all kinda mean one thing:
its its the whole 73 where the
puny think is is bothering at this point cock
one thing I know I could do if I have a list use
I think also what on passable walk
end up walking okay it does is used a Jim
here should be 8ee all their
edifice equals
here that is true and select for me
all users who main this was
used letters whole Ruby code and that's the way we're a group photo every day
ever walk
novice see all-star that we have to be a so it's also
I ruling drawn to the what we are doing things but
another database is therefore models are happy that somehow
but would if wouldn't it be nice
yup there's a wave useful app
if record that would be so cool
so we could think this users of the lock give up what the hell's our criteria
we quote everything Ruby everything would be beautiful and we're all happy
never have your seaport yeah so it looks like
or writes a lot here's my first
tapped I
here some cough move from the audience depicts the problems with this poem
a folder
work pretty
for of
hot no for
okay its hugely inefficient what what what I'm doing I'm
grabbing all the users from the database and they're not putting the
analyst so I have a million users I just having
array with a million entries in nice or who have for the him for users
whose name is actually jim not really
efficient not official all its low
marked people's love it after would kill disco your databases are actually
as much as I really if they basically are think they do things
very well be have really efficient algorithms for searching
data in the table a indices set up the practice of
at work using database up all for this
just for the sake of being able right up group so
got up got this problem want to do everything Ruby
but I also want to take advantage database as well saw what can we do
well I'm gonna hypothesize
a magic implementation up for what and the magic he is OK
we have this little magic method called translate block
we pass up the block it will come up
the resolve this method will be
a condition we can pass into rails
and real and sequel and the database
all the good work that it lights or presley the former towards
and then the database does its job we're still
writing everything that we do Ruby that would be really cool
so I would
well I could think of
ample avoid my property we could actually part that quote how many people
have written
the par-4 great
was a lot of fun
are for ya probably was but also a lot of work
just to solve the problem right for writing a rooftop
that's just how many people have written a Ruby park
perhaps word
you that's a hard thing to do
ruby is an interesting like with part that after I think performers up there
that that will handle some about for
pool work
for me because I i'm looking for the simple right in the evening well
oh yeah this is this is what rock this is a small strip
a coat from the Ruby parser noted review itself so
I couldn't get in there there's a library call parth
ass here on for war
hip bypasses the whole out like are
requested by say what there's one soco the
really really really knows how to are through be thats Ruby
so what's what Ruby parce and we'll go into
its gots Andrew lipow the abstract are bbbbb are
the par-3 that's built into Ruby itself wats pose a
into rebuke who and it's a really cool like
yup I really recommend doing that factors such a good idea
someone else had that idea up and if you look up to him
mission project are terrible blog this URL
he does exactly that fact he writes a berth lock
using par-3 to get up-to-date up so
if you want to do this is a good way to go and look at that library I think with
I'm going to start up for all users were it's really quite yet but we're not
going to do it that way
me because someone else is doing it but of Pau
why don't we just execute code
all XQ
Co what's gonna happen keep that bought mine for just a second
and this is a I R P such for your
limitation star RB we load
a Weiber called I no on I create another cop
people so we're going to be dealing with no table node is a
whip are you a little bit 10 yes the table node is the No
remember a database so we talked to remember the name up
users which is the baby over database table
backs are record all restore that in a variable called
user and then we
whole about that or user method is .name
restore the result about that in a variable call result
Reprod up all I would convert result was throwing
recap users got me
that's really interesting
I created on our I call the method on top
the result of calling wrap it up like Britain result own
through it tells me what level I call
one topic but all actually it
act but does something a little bit different
for it ver that method call
user to see all right references the tabletop
cool
I can go yet feel
reference from within just by using the stable though
and just to illustrate farther I also ask for 8 field user
want to convert the resulting back to a string I get users
now a so I can reference I can build up sequel
for a very simple method all and your publication the people know
is trivial it's one its two classes the first one is people vote which were
using directly
it just remembers the name of the papal that were referencing
and when you convert back to restrain
there the bottom of the screen you see that it just returns the papal main
trivial Co only minor tricky stuff in it at all
is about the missing call and when we yep
a method or are people know that sells
I am going to remember map it was just
all by creating a new opera calling map it would
the method noble recall me able note
possum up that was just called on me Richard that
as resolve the matter call itself and so the
method no is the resolve calling about the daughter Ruby object
his method no again really trivial
which is reports the Opera the method name and when asked to convert to a
string
does exactly that the app is all a coat the
implement RRP session ask who'll
were Parkway converting grupo
see cover method called
objects to see or fragments reference
your or arm people's
but to be a little bit more just for
your reference are here but worth
how do we say usually equals Jim how to record that
approves like that into sequel
well you know walk double eagles is just about the
an I could be five EE also no
I can see something this violent worth or
and I can create a new I don't know
with that you're gonna see a lot of different note flap implementation
all the binary op know who did the binary opto
was looking for double equals seaport will look
up that would be a single all and I have to remember the left hand side on the
right hand side
up also a record that we initialize
or have children implementation by Marriott or
record the operator the left right hand side
have been weak US the whole thing we just convert it to a
see or fragment that have currencies around the we'll have to worry about
going back for upper and we have the
left hand side revert to a string operator converted to string
and the right now it's like bird just right
Wow Barnardo back by Yuri of know it was
the you as well about very so
reiterating capture the method
in the note buyouts greedy note of it represents a
miss lies that note of liquid things that it needs to go
just return the app implementing the note is pretty trivial
and running this now greater cable though
we execute code user H equals fifty
and store that address walk
room convert res 12 string it says
users got a you see all
where clause up Ruby pro
right there try to get let's do the same thing with mister now lets a user dot
mainly was Jim
government will jump and when we convert their the result of that most rain we
get. users not made
single people jump who
I got a prop
yeah we have no quotes Roger ap
with easily that small matter program
what we've done is that we're not think which we're taking Ruby African just
don't
about a string up really good
differentiate conversion we need to capture converse Ruby objects
the sea block numbers just
US on a number that's fine over string recover reviews true
to AC of rag really wanna put both the route
so we need to surprise Perino class for those
was pretty liberal no playoffs and that's just handle simple things like
integer
burt directly at the sequel and then we'll create
a string node that handles rivers and streams in the sequel strings it does a
when hope you have pictures at single quote you cite strike
probably wanna make that a little bit fancier Campbell
all so thus keeping that string as well
be aware about but we're keeping it simple here
now we have a problem
I've got a bunch of different well got last
literal string but I have to make sure like right
Ruby up we have to look at a Ruby up
we have to make a decision artwork by that Ruby object
and come up with right kinda Ruby op
we could do this I could create a method called rapido
takes an article from looking statements you know
BAP and this will work fine
arm I don't like this your
reason yes I've got a wonderful over
which objects know how babe himself
things quite alright ask centralizing all discover
the one place 10 it distributed one we object to every
object knows how to convert itself into it sequel
no so why would rather do something like this
open up object class creepy
as a sequel note up and it is wrapped in ER preoptic
as a liberal no min string which is a special place I wanted
Bowl I just rap strings Op
any string no and recover that as well they're both coal
as a sequel the key is we're opening up
core classes your wanna be purple with the bay up
this is perfectly fine Ruby but whatever
careful because your opening up for classes at the Community
with like so that's widening the
as AC what no talk hope that name was
unique about that I probably would collide with any other
arbitrary review library attic big sister
up just be aware that if you
up and now converting any object to me
no is really really easy just after up
gimme yourself as the proper no
only did it worked with no little bit it needs its own as a sequel
no about that this is already a node
this report itself that works out for what part mean we need to use this
when we create in binary of
I'll no we need to make sure that be
right hand side up the operation converter
at the left hand side is a note of already that's why we're here
Expo we don't need convert self it's already have no
other may or may not be ago just convert would know
now when we run this
single quotes that's really cool
we can convert Arbor Peru biko to sequel
just for executing the rupee Co
okay ok love to be with us example well
there's lots of operators we need to patch up the leak was not the only one
we need to hit all the other
greater than life I'm a grew equal to
people to all the of Minas
star provide operators and a bunch of other operators you might want to
consider
on things like no question mark
no question mark was the rubidium for doing that perhaps note
pic no Corp marker like that you is
naw okay so there's up perhaps
pattern matching should be converted the sequel like
operator maybe maybe not I don't know.
was all kinds of things that we could at the apt been if we wanna write somewhat
it's pretty much just this supply north africa
you to ask all our people up result wat
take the block we execute the ball passing year
eight people know object as the
all as the argument so would we evaluate our condition the condition that the
value we did against
the people or objects builds up a sequel called
as we build right weary when we're done that result car
we take it converted to string which I for got put into the slight
and passed it to fund all we've got are
selector we got our magic ver to
are report to sequel
cool so why don't we do this
or problem well there's a number up
most operators are from our community up
so we can see user not name is Jim or we can see Jim
probably was user got a the first would work
certainly has a small problem that double equals is being stepped through
the Strait
wire during doesn't know about 20
so it's going to return true or false it's not going to refer
for proper note of it after that so bad is the problem can't play with that
override are double equals
and capture the app that we're playing were all
really fundamental depositions Ruby I'm that are
all that so battles
that's a small problem either solver site were actually if you have
mathematical operators like plus mines
both actually payable are the whole
coercion problem by I a protocol called
or so in Ruby if you have a pics no
41 yeah at the gather have to figure out what
crop result for those things participated warm-up
or worth figures up rock of past do that
you actually are you over here use
in Bow and make sure that notice on the right hand or left their flights the
work
up so mathematical operators are okay
some of them such as double artwork right
as a corpus work
but here's a bigger problem I think this is really the killer
that really stop suspected to be useful for sequel
and that's the double in person and a double work
a bar operators or
well not about you cannot override them the reason is
that double am double vertical are and the
or operator short-circuit
their evaluation they can determine the true
true or false overexpression just by looking at birth of around
you ever valuing second I'll
those difficult to find that program semantics any
about that there for Ruby does give us away hooking into
those operators as override you cannot overwrite
double am double vertical bar
up you could use going single critical parts that
then we have to write special Ruby old cycle select walk rather
record that's a little bit a comfortable for me
a bigger problem also is that a but not bang operator
be back of greater all sort of override
you never say not addition always say bank in ethics Prof
for true or false it will never think that
bird not no is no way of doing that
all like whites for
can't change can overwrite so that's a problem
and so I would really recommend this technique for a sequel it's a great
example
kinda motivate the problem but I'm kinda throwing this out
really hoping someone will have a great idea for using the I think the beautiful
part
up it's a solution or working for problem perhaps
all maybe evaluating expressions and
rafik are some a mathematical analysis useful
maybe you have a problem in Cuero able to be really useful
you but technique is happen room
code execution Tracy markets call and building up a structure about
hell what's happened in the past arm
actually use this very mall limited way
influx law what's marked with the ability Rick or
all transactions as they happen on the box awaited
playback you have a verified
good way of doing something and a new way of doing something
verify that the in Iraq law a very limited
half bath up upping like
arm I got this idea actually from a small
like war which does this
smallpox very similar is also a library in ruby port part
Syria does this as well suffers from the problem
you've mentioned before you look close
saw
advance Ruby class design what we were always follow up on code
that was actually my goal for today I think we really should know about that
that language shapes the way you think that when your roomie
work or ruble fight solutions to your problem and in doing so you will perhaps
understand the corners of your life you coming to Ruby
from Java for C pas also from a different language a news Ruby
very quickly but Appling were those or repeat
what weapons or learn about meta programming to learn about redefining
operators
you're gonna miss law up good solutions
your problems to you can't get that or language
all your belts are really encourage you to you
going research language under spam was paid orders
also wanna courage go ahead
outside the box work for those unusual
our solutions you don't need them all the time that don't need them
both both pro-growth like this I would be very
very word but there are occasions when needing special solution
Ruby up in a fine that's possible solution for you so go ahead
think outside the box your cock after all up three years ago someone had
not stopped all the POC with regard to web frameworks
Arts will be right this kind of boat
so thank you very much

No comments:

Post a Comment