00:00:19.050
alright good afternoon welcome to the
00:00:23.050
dark side of ruby and since it's just
00:00:26.589
before lunch it's my job to build up
00:00:28.869
your appetite well I've flown in from
00:00:32.470
India and my company's name is Joe
00:00:35.379
software we've been doing this for the
00:00:37.570
past seven years and it's been fun but
00:00:41.010
after seven years you realize that like
00:00:44.859
in every marriage the going is good in
00:00:48.370
the start kochi sons about for that but
00:00:52.570
after a few years you start to
00:00:54.039
understand that there is another side to
00:00:56.800
your spouse so my aim here is not to
00:01:02.129
ridicule or not to you know chastise
00:01:06.550
ruby but to see the cured the weirdness
00:01:11.200
of ruby so that's exactly what i'm going
00:01:13.570
to talk about it's nothing scary I still
00:01:18.760
love Ruby I still work in Ruby every day
00:01:21.240
but it's the weirdness that I want to
00:01:25.030
point out and if any one of you all get
00:01:29.350
the ha moment then we are in sync now
00:01:37.960
I've been seeing that not many people
00:01:40.900
are asking questions after the talks so
00:01:44.980
I said let's reverse the tide most of my
00:01:49.390
slides are actually questions for you
00:01:51.640
all so there's no brownie points for
00:01:55.720
getting the answer right or wrong but it
00:01:58.690
is for you to understand so I might ask
00:02:00.730
you array what do you think we don't
00:02:02.260
raise your hands and try to be as
00:02:04.570
interactive as you can now such a kind
00:02:08.740
of talk is sometimes difficult because
00:02:10.210
we I don't
00:02:11.220
no the the audience come from a broad
00:02:13.710
spectrum which is either beginners or
00:02:15.810
some experts but you know I realized the
00:02:20.610
slides where the slide is the dark side
00:02:23.160
of ruby in it lined with the dark side
00:02:25.230
of the moon so I tagged my slide and I
00:02:29.550
have my transformer friends to help me
00:02:33.150
bumblebee for all the sign of questions
00:02:35.550
which are like a beginner level
00:02:36.900
questions and Optimus Prime for all
00:02:40.740
kinds of little crazy stuff so in case
00:02:43.290
you need to tune out during these
00:02:45.090
questions that's ok so without further
00:02:47.670
ado let Christian let's talk about the
00:02:52.650
infamous infinity now everybody let's
00:03:00.540
time I'm being either you can see
00:03:01.980
bumblebee there it's not simple stuff
00:03:03.300
first everyone knows the output of this
00:03:07.640
no points for that we all know its
00:03:09.840
division by zero but what about this the
00:03:13.860
hint is in the slide guys infinity
00:03:17.660
alright so let's see what is infinity
00:03:20.370
and infinity turns out to be alright so
00:03:26.760
that's interesting so like hmm but then
00:03:29.790
most of us already know that infinity is
00:03:32.160
a constant defined in the flawed class
00:03:34.340
well alright so I said let's go find it
00:03:37.170
I actually went and dug into the cord
00:03:40.680
and found it infinity is defined like
00:03:44.310
that a ruby but you know the interesting
00:03:46.260
part here is defining missing not H so
00:03:49.769
that's pretty cool so I had no idea
00:03:51.830
infinity was actually missing well these
00:03:56.820
the kind of things are not going to keep
00:03:57.930
talking about i'm going to ask a lot
00:03:59.370
more questions so I'm just warming you
00:04:01.260
up into the game the adrenaline rush
00:04:05.959
anyone knows the output of this on what
00:04:09.780
this really does it's a number
00:04:12.630
conversion to a string as an octave so
00:04:20.310
far so good no problems at all what
00:04:23.160
happens if i push the limit that's still
00:04:29.400
a number so any one of y'all next time
00:04:34.140
sees you know names like a Strix publix
00:04:37.530
get a fix it could be a number well
00:04:42.930
let's push it let's push it further what
00:04:46.530
happens now that's an invalid radix now
00:04:54.810
that seems strange but here's the trick
00:04:57.890
radix 36 actual you work because I have
00:05:00.450
26 alphabets in 10 digits so I can go up
00:05:04.200
to 36 so if any one of y'all is lucky or
00:05:07.680
smart enough to get a new alphabet
00:05:09.000
introduced in the English language we're
00:05:12.390
going to have radix 37 well let's move
00:05:16.860
on to our star of the show the splat
00:05:22.140
expander we always use the star in lots
00:05:24.870
of places multiplication but anyone
00:05:26.580
knows the output of this sound pretty
00:05:32.820
simple it looks I have said it's a hash
00:05:34.380
so it should be a hash which looks
00:05:36.420
pretty straightforward but it gets a
00:05:39.480
little more weird after this what about
00:05:41.850
this now I got not pretty simple stuff
00:05:45.210
man what do any takers for this one good
00:05:51.450
thing will it be three arrays of one two
00:05:53.880
and three anyone will it be one array of
00:05:58.080
1 1 1 2 2 2 and 3 3 3 or will it be one
00:06:02.669
two three yeah I see some warning heads
00:06:05.310
and that's perfectly fine which see
00:06:07.530
makes logical sense right what about
00:06:09.900
this hmm
00:06:16.500
the same array interspersed with percent
00:06:19.220
pop swing concatenation so what you see
00:06:24.690
is not always what you get let's do a
00:06:29.640
little more stuff here some more fun
00:06:31.380
this is a lambda what if I call this
00:06:38.660
what is the output here and this is
00:06:42.810
straightforward Ruby stops really
00:06:44.250
there's nothing weird in this it's five
00:06:46.830
yep because you're looking in the first
00:06:48.840
I gave the hinge their middle second
00:06:51.450
last and last and the outputs pretty
00:06:53.430
straightforward but the interesting part
00:06:55.950
here is in the call we all know that we
00:07:00.720
call block like this right what happens
00:07:03.000
if I do something like this pay close
00:07:04.680
attention to this part is that a syntax
00:07:09.150
error anybody syntax error that actually
00:07:15.419
works because that is a syntactic sugar
00:07:17.100
for calling a block and we will see a
00:07:19.410
little bit of it later now speaking of
00:07:22.620
syntax this should be interesting and
00:07:25.890
now we'll actually see how many were
00:07:28.950
really paying attention to Kochi sounds
00:07:30.990
talk in the morning we're all used to
00:07:33.930
seeing functions like this right hey
00:07:38.340
coach is unsure is this early in the
00:07:40.740
morning let me what argument introduced
00:07:44.490
in Ruby tu lado use it because when
00:07:47.910
you're looking at variables a and B what
00:07:51.300
looked like parameters there can be
00:07:52.770
actually accessed as local local
00:07:55.050
variables inside the function foo and
00:07:57.470
then special attention to this part have
00:08:05.460
you written a syntax like this anyway
00:08:10.020
takers if this is going to be a syntax
00:08:11.400
error but this is a mandatory keyword
00:08:14.460
argument it's part of the show because
00:08:17.100
of the morning talk but this is the
00:08:20.160
mandatory ultimate introduced you to be
00:08:21.419
to drop one well let's see some more
00:08:26.490
syntax
00:08:29.420
again pretty simple stuff
00:08:31.620
straightforward method nothing fancier
00:08:34.200
right how do i invoke this method this
00:08:39.840
works right right but when i try this i
00:08:45.110
get a syntax error now why do i get a
00:08:50.130
syntax error here did that look a little
00:08:55.290
weird now i say oh okay let's do it
00:08:57.360
again and all I've done is close the gap
00:09:03.440
and this works perfectly fine why did
00:09:07.260
this happen it's Ruby we say that you
00:09:11.460
can call a method without passing the
00:09:14.190
round parenthesis so if I put a space
00:09:17.280
there the parts is actually going to
00:09:18.930
interpret that as some sort of
00:09:20.850
evaluation and then if I written that as
00:09:24.510
1 plus 2 it would evaluate and pass 3 2
00:09:27.630
method foo but since it came across of
00:09:29.760
karma he says dude I have no idea what
00:09:31.920
you're doing so sometimes what you think
00:09:35.220
works should what doesn't really work
00:09:38.210
well I've used the title syntax but now
00:09:43.950
is a simple question for you guys and
00:09:45.750
I'm pretty sure you'll be able to answer
00:09:47.280
what is a in this particular slide let
00:09:54.870
me give you some multiple choice how
00:09:57.510
many think it's an array is an array you
00:10:00.720
can raise your hand you're not wrong oh
00:10:03.950
come on how many think it's an array it
00:10:06.540
is an area right but can it be something
00:10:09.120
else can it be hash it can be a hash
00:10:15.590
right because it Ruby keys can be
00:10:18.480
anything can it be something else of
00:10:23.220
course it can be a string but that was
00:10:24.600
obvious right something else how about
00:10:28.710
it being a proc that actually works
00:10:33.450
because I can call a proc using square
00:10:36.390
brackets well
00:10:39.850
let's get to some sort of case
00:10:42.529
complexities all right there pops up
00:10:46.190
optimus prime time to take a break well
00:10:50.480
we look at the we all used to using K
00:10:53.810
statements typically we use it for
00:10:56.120
simple stuff checking that you know if
00:10:58.370
if some value exists if there's a number
00:11:01.190
of the string but you know what I can
00:11:03.560
actually use and call a method here this
00:11:07.850
particular method effectively turns out
00:11:10.610
to be evaluated like this so I can
00:11:14.029
actually have a method with the case
00:11:16.250
equality which is passed the parameter
00:11:18.470
in the case statement so using this I
00:11:21.529
can use this case equality operator to
00:11:26.600
actually evaluate something but then
00:11:28.000
what is it the case equality operator is
00:11:31.399
just an alias to the call method so
00:11:34.540
that's how we get the output and this is
00:11:36.980
the evaluated form of that case of
00:11:39.410
equality operator which in effect means
00:11:41.870
that if I really want to change the way
00:11:43.699
I want to compare two objects all I have
00:11:46.639
to do is override the method and I can
00:11:50.420
just do what I want but speaking of the
00:11:54.440
case equality operator speaking of
00:11:56.029
equality let's have some more fun what
00:12:03.860
is the output of this don't think too
00:12:07.699
much if this was not true would you even
00:12:11.930
use this language it's true all right
00:12:15.649
don't worry about it's true what about
00:12:17.540
this and he take it javascript people
00:12:26.320
somebody that's type check come on guys
00:12:29.470
how can it be false I just said it was a
00:12:33.250
case equality operator so it just
00:12:35.440
compares the two a common mistake made
00:12:38.200
in Ruby because typically because of a
00:12:40.930
JavaScript bias if we feel that it's a
00:12:43.660
type equality check you check the data
00:12:45.520
type of the object stuff oh it's as
00:12:47.860
simple as that it's just a case equality
00:12:49.270
operator it's true Oh what about this
00:12:52.770
eql does seem like equal it's true but
00:13:01.210
with some caveats it's just an alias for
00:13:03.670
the generic equality operator however
00:13:08.400
however it's overloading only for the
00:13:11.470
numeric class most most classes just
00:13:16.000
alias it off to the generic equality
00:13:18.430
operator what about this it's got to be
00:13:23.590
something different right when in doubt
00:13:27.180
choosing their true or false what's the
00:13:31.540
output people nil that is not binary man
00:13:37.620
but what else it's got to give some
00:13:40.360
output this is anyone for true raise
00:13:45.040
your hands don't be shy anyone for false
00:13:50.250
and the rest no this is actually an
00:13:57.910
object identity check so when you're
00:14:00.700
using equal ? it's actually checking the
00:14:03.760
object identity which is why it's
00:14:05.770
different so what's the output Europe a
00:14:12.090
people have been listening and what's
00:14:15.040
the output yer now come on he'd not be
00:14:19.960
listening true or false the two
00:14:24.130
different objects true false well in
00:14:28.000
this case it's true cheese why because
00:14:33.070
object ids for fixed nom are calculated
00:14:37.690
like that so if you have an object
00:14:40.080
for a string it gives you some big
00:14:42.210
number but object ids for a fixed num
00:14:45.180
are just x 2 plus 1 interesting right
00:14:49.710
now i urge you to go and ask few of the
00:14:52.470
core ruby committers here why is the
00:14:54.690
soul because this is pretty complex
00:14:57.620
actually not quite here's another
00:15:00.450
snippet which I was just pushing the
00:15:02.100
limit to find out on a 64-bit machine if
00:15:05.690
I do that to raise to 60 2-1 it gives me
00:15:10.440
a fixed number 2 raise to 62 gives big
00:15:12.570
num two different classes now anyone 62
00:15:18.450
I'm sure it's nothing to do with a
00:15:20.640
random number because it could have been
00:15:22.170
able to beam 42 and should have been the
00:15:24.570
answer to life universe and everything
00:15:25.710
but we chose 62 it's a 64-bit machine
00:15:30.000
the last bit of fixed number actually
00:15:33.270
determined whether that is going to be
00:15:34.680
used as an object ID or not for other
00:15:36.840
fixed nozzle so you have only 63 bits to
00:15:40.620
play with and that's why that's how it
00:15:44.250
works well let's try and see if we have
00:15:50.550
hit a jackpot today so I wrote some code
00:15:56.030
and my code is simple it's three pools
00:16:00.600
for a jackpot three poles should tell me
00:16:04.670
whether I have actually got a jackpot or
00:16:07.050
i am the sucker but I've written the
00:16:09.900
code in a slightly different way with
00:16:11.250
special mention about the curry method
00:16:16.520
now seem different and here you can
00:16:20.400
notice that I've actually called a block
00:16:22.260
of code using square brackets I am
00:16:26.360
calling the same method but the
00:16:31.260
evaluation is slightly different when I
00:16:32.760
use the curry recipe with curry unless
00:16:38.160
all the parameters but the block
00:16:40.380
parameters are satisfied it's not going
00:16:43.020
to evaluate it is only retain you
00:16:44.340
another proc so it evaluates it twice
00:16:48.780
when all the third time the parameter
00:16:51.270
actually comes in
00:16:52.540
it's the block well there you have it go
00:16:56.560
and cut it and so so you think you can
00:17:05.350
tell protected from private somebody can
00:17:13.560
okay well what do you think of this your
00:17:21.120
will I save Private Ryan traditional
00:17:28.240
object-oriented methodologies tell us
00:17:31.180
that private methods are not inherited
00:17:33.990
right in Ruby all methods are inherited
00:17:38.910
private methods are indeed inherited and
00:17:42.600
though this sounds very simple though
00:17:47.560
this sounds a little astounding to a few
00:17:49.240
people here my entire Park my
00:17:52.810
preparation the dog started off at this
00:17:54.310
point because let me take a simple pole
00:17:57.820
how many of you do not work in rails but
00:18:02.560
work in Ruby there be a I could count on
00:18:07.570
my fingers the problem is all of us
00:18:09.610
started working in rails and when you
00:18:13.090
start working raised it's so cool it's
00:18:14.830
so awesome that we tend to forget that
00:18:17.980
it's also very important to learn Ruby
00:18:20.320
so when I started digging deeper with
00:18:23.380
some of our developers yes we had a ruby
00:18:25.210
on rails development company and I
00:18:27.100
started quizzing a few people I realized
00:18:29.290
that people did not know this and I was
00:18:32.290
I was stopped because if we do not know
00:18:34.690
Ruby then we cannot be good rails
00:18:36.190
programmers but since Reyes just makes
00:18:39.130
it work the rails magic people continue
00:18:41.530
hence the essence of my talk was to open
00:18:44.470
your eyes to these things having said
00:18:46.720
that and we know that private methods
00:18:48.730
are inherited then what is the
00:18:52.210
difference between protected and private
00:18:54.070
anyway we come to it before that quick
00:18:59.320
thought we always use include read for
00:19:02.980
any including any module but what is
00:19:06.080
include is it a keyword is it a method
00:19:12.100
it's a method which is defined as a
00:19:16.549
private method private instance method
00:19:18.950
in the class module that's how you can
00:19:21.620
actually use it there well like I
00:19:25.850
mentioned earlier if all private methods
00:19:28.700
are inherited then what is protected in
00:19:32.419
Ruby because again traditionally we've
00:19:35.659
been taught that Oh protected methods of
00:19:39.080
deals with inheritance all protected
00:19:41.720
methods are inherited in the subclasses
00:19:43.669
but you cannot call them on the object
00:19:45.590
but our private methods also inherited
00:19:48.169
they cannot be calling the object so
00:19:50.600
what is the difference between the two
00:19:52.149
Ruby takes a very pragmatic approach and
00:19:56.740
protected works with objects and not
00:19:58.850
classes so you can invoke a protected
00:20:03.080
method on an object if it's in the same
00:20:04.909
lineage and then you go what well it's
00:20:12.080
better with an example so check this out
00:20:14.240
I actually have initialized optimus
00:20:17.870
prime here but can i call the protected
00:20:21.169
method on it i have an activity accessor
00:20:27.860
nique there for nickname for brevity can
00:20:32.059
i call the protected method on this
00:20:33.380
object that it does what it does work
00:20:38.600
because it's suppose it's not supposed
00:20:40.399
to work you cannot directly call an
00:20:41.960
object you cannot call a protect method
00:20:43.639
on an object however what happens if I
00:20:49.010
change my code a little bit I've
00:20:51.230
actually defined a public method called
00:20:53.210
fights as passed it another Autobot
00:21:02.919
special attention again this particular
00:21:05.090
piece of code have I just called a
00:21:10.610
protected method there so is possible to
00:21:14.990
call protected method on an object if
00:21:18.700
both the objects
00:21:20.419
come from the same hierarchy the same
00:21:22.309
lineage so if two objects have the same
00:21:26.149
parent class or the same class or their
00:21:28.309
hierarchy you can actually call him as
00:21:30.200
long as you're within the class well
00:21:34.269
moving on hmm this Ruby have any
00:21:39.739
keywords yeah ruby has keywords so then
00:21:45.259
I want you all to tell me whether I'm
00:21:47.149
serious or I'm joking can i define a
00:21:52.909
method called true this code actually
00:22:01.009
works and it's not that i can define a
00:22:03.799
method true i can define a method called
00:22:05.450
death i can define a method called if i
00:22:08.889
can define a method by any freaking name
00:22:12.590
the high part whether it we use it in
00:22:14.720
standard ruby or anything but try
00:22:17.029
defining method with end def end and
00:22:19.850
then you're going to have a little bit
00:22:22.669
of what we call problems because i got
00:22:26.840
to banish my tongue here ok so if you
00:22:29.989
try and define any keyword as a method
00:22:32.480
inside your class it works but again it
00:22:36.259
will work only in your class try this on
00:22:38.029
I RV and it won't work try this on i RV
00:22:42.440
it won't work put it inside a ruby file
00:22:44.899
it works because I RV itself is a ruby
00:22:48.679
program so evaluating this outside its
00:22:51.470
scope so put it inside a class and
00:22:54.320
everything works fine well who thought
00:22:58.369
it was tacked to deep end went into a
00:22:59.960
recursive loop well it doesn't but can
00:23:02.570
it can i how can I make this can I make
00:23:06.859
it go into stack too deep if I had
00:23:10.509
self-taught true inside my method fault
00:23:12.710
if I had written self dot true and
00:23:14.799
inside my method def true I'd written
00:23:17.330
self-taught false it would actually go
00:23:19.369
into a recursive loop and have a stack
00:23:21.200
too deep error all right super up there
00:23:26.869
you have it we all know what super dues
00:23:31.070
right
00:23:32.850
super if I try to try out the score
00:23:36.730
you're here I've written a class in
00:23:39.309
which have defined a method called
00:23:40.390
search and in the child class i define
00:23:44.470
another method called search in which i
00:23:46.990
take a mandatory keyword argument and I
00:23:49.570
call super should it work I thought it
00:23:54.370
should work but it gives me an error
00:23:57.040
which I couldn't quite follow in the
00:23:58.809
start wrong number of arguments 140 now
00:24:03.490
it's actually given me and ever exactly
00:24:06.550
where super has been called and it says
00:24:07.990
wrong number of arguments 140 I can pass
00:24:11.740
any arguments there so should a big 0
00:24:14.080
for 1 with ID pass any argument did I
00:24:17.800
pass they're not passing out dimension
00:24:20.640
but if i change my pole a little bit
00:24:24.540
again notice all of added is the
00:24:27.720
parentheses there and this code works
00:24:30.340
fine so if you really thought in ruby
00:24:34.300
that you never ever really need to use
00:24:37.090
the round brackets and it's always
00:24:38.470
optional think again think again well if
00:24:46.630
this was good enough sauce for you right
00:24:48.370
now let's move on to modules and see how
00:24:52.900
module mix-ins are sometimes pretty
00:24:54.309
funny and this is the sample piece of
00:24:56.679
code erase this sample is pretty simple
00:24:58.780
and nothing fancy here I've just defined
00:25:02.890
three superheroes Superman Batman and
00:25:06.790
Iron Man all having the fly method in
00:25:08.800
them alright this is how I invoke oh
00:25:14.890
this is how I call a method or it I
00:25:17.070
created new tiny man right I want him to
00:25:21.100
get the super powers of Superman Batman
00:25:23.500
and Iron Man and I want to make tiny
00:25:25.809
man's fly and this just works fine no
00:25:29.410
questions so far however if I go and
00:25:33.460
change my code in my modules I do the
00:25:37.240
unthinkable and add super in my module
00:25:42.450
what happens now
00:25:48.690
we know that modules are not classes we
00:25:52.060
know that modules cannot inherit from
00:25:53.980
other modules so if are all super in one
00:25:57.280
of the methods what happens lo and
00:26:02.380
behold this actually works and now I was
00:26:08.410
stopped how like to there's something
00:26:10.990
wrong here how can this really work so
00:26:16.600
what happens here is when this class is
00:26:20.920
instantiated the meta class hierarchy
00:26:25.030
that is built is actually done on the
00:26:27.010
fly so my inheritance hierarchy actually
00:26:30.400
flows in this particular order which
00:26:34.450
means that if I just change my module
00:26:36.820
inheritance structure I include a change
00:26:40.450
the inclusion order I actually get a
00:26:43.390
different inheritance hierarchy and that
00:26:47.170
is why super actually works this is to
00:26:51.370
be done with a pinch of salt this we
00:26:53.260
found out the hard way we found out we
00:26:55.960
found this out in actual production code
00:26:58.000
because somebody made a mistake and
00:26:59.740
somebody want to take a shortcut and had
00:27:01.240
it super in of the module methods and
00:27:03.180
hit the roof that we were wondering
00:27:06.070
how the hell can this happen till we dug
00:27:08.410
deep and found out all right there is
00:27:09.940
more to Ruby than meets the eye well if
00:27:13.590
this wasn't enough what about something
00:27:18.790
like this I want to share a pig no this
00:27:23.080
is my module Megatron who is powerful
00:27:26.590
what is evil now in typical Indian style
00:27:31.750
I have my own class called her new man
00:27:36.120
okay I want him to be as powerful as
00:27:39.550
Megatron but if I include him Hanuman is
00:27:43.660
going to be evil and i am going to be
00:27:46.210
kiddin so I cannot let this happen so
00:27:49.030
can I cherry pick from the Megatron
00:27:51.820
module I want the power but I don't want
00:27:54.760
evil
00:27:58.020
welcome unbounded methods so i can
00:28:03.130
actually cherry pick from my module find
00:28:05.980
the method it's an unbounded method i
00:28:08.440
bind it to my own object i can call it
00:28:12.670
and in this particular case Hanuman is
00:28:16.050
powerful but he's not evil and if this
00:28:23.680
has not been tier appetite there are a
00:28:25.210
lot of other things that happen in Ruby
00:28:27.040
but I have only half an hour for my talk
00:28:28.840
and I have a few minutes for questions
00:28:32.350
and if you are not gonna ask me
00:28:34.780
questions I could probably ask you a few
00:28:36.160
more well that's all folks