< Return to Video

Garden City Ruby 2014 - The Dark Side of Ruby by Gautam Rege

  • 0:26 - 0:28
    All right, good morning people.
  • 0:28 - 0:33
    After a bright talk this morning by Ajey,
    it's
  • 0:33 - 0:38
    time to get dark and dirty, OK. So I'm
  • 0:38 - 0:42
    from Josh Software, and it started in 2007.
    These
  • 0:42 - 0:47
    are my Twitter handles. And I'm also an author.
  • 0:47 - 0:50
    I have a couple of books which are out.
  • 0:50 - 0:54
    Well it doesn't look so dark anymore, does
    it?
  • 0:54 - 0:58
    So what is my talk gonna be about? Don't
  • 0:58 - 1:02
    worry. It's not gonna be anything scary. I
    love
  • 1:02 - 1:07
    Ruby. But as with every marriage, and I hope
  • 1:07 - 1:09
    there's no Ruby in the crowd - OK. As
  • 1:09 - 1:11
    with every marriage you also need to know
    the
  • 1:11 - 1:14
    other side. You know the side you were -
  • 1:14 - 1:17
    you didn't know when you were just going around.
  • 1:17 - 1:20
    But it's the same thing with Ruby. You have
  • 1:20 - 1:23
    to know what you're looking at. And my talk
  • 1:23 - 1:26
    is going to be about the weirdness and the
  • 1:26 - 1:30
    gotcha moments. In short, in my talk, if you
  • 1:30 - 1:36
    find 'Ah-ha!' moments, it's working. Oh, we
    have a
  • 1:36 - 1:40
    varied audience today. So to ensure sanity,
    I have
  • 1:40 - 1:44
    tagged my slides. My slides are tagged with
    my
  • 1:44 - 1:49
    good friend bumblebee to keep it neutral,
    that these
  • 1:49 - 1:52
    are for beginners. So you must pay attention.
    And
  • 1:52 - 1:55
    the experts, try to give the beginners a chance
  • 1:55 - 1:59
    to give the answer. And the experts, you know,
  • 1:59 - 2:03
    take your inspiration from Optimus Prime.
    And the beginners
  • 2:03 - 2:06
    can choose to tune out, but whatever you learn,
  • 2:06 - 2:10
    the better. So let's get started with the
    infamous
  • 2:10 - 2:17
    infinity. Now, we all know what infinity is,
    right?
  • 2:18 - 2:21
    And since we are all programmers, obviously
    we should
  • 2:21 - 2:24
    know the answer to this one. What's the answer?
  • 2:24 - 2:29
    There are no surprises here. No surprises
    there. No
  • 2:29 - 2:33
    division by zero. We know this. What about,
    what
  • 2:33 - 2:40
    about this? Oops. This actually works. So
    we know
  • 2:40 - 2:44
    that everything in Ruby is an object. So that
  • 2:44 - 2:46
    looks like a, that looks like a class. So
  • 2:46 - 2:48
    let's see what the hell that is anyway. If
  • 2:48 - 2:50
    you type this out, what do you think I
  • 2:50 - 2:55
    get? So... what's going on here? So infinity
    is
  • 2:55 - 2:57
    a constant define in the float class. Where
    do
  • 2:57 - 3:00
    you see it, you don't see it there. I
  • 3:00 - 3:02
    can use this constant to form range comparisons.
    I
  • 3:02 - 3:05
    can use it with equality operators and stuff
    like
  • 3:05 - 3:09
    that. Imagine three equal to equal to infinity.
    But
  • 3:09 - 3:13
    it works. Oh well. So. This is the way
  • 3:13 - 3:16
    I get people warmed up. Let's do something
    with
  • 3:16 - 3:19
    more of an adrenaline rush. Base conversions.
    It's a
  • 3:19 - 3:23
    lot of fun. So what do you think is
  • 3:23 - 3:27
    the output of this particular thing? Now,
    I don't
  • 3:27 - 3:30
    want all the math geeks going to their calculators,
  • 3:30 - 3:35
    but essence is converting a number to a string
  • 3:35 - 3:39
    in the octal format. So again, no surprises.
    Should
  • 3:39 - 3:45
    be fine. But let's push the tempo. Now what?
  • 3:45 - 3:51
    That, actually works. The next time you are
    reading
  • 3:51 - 3:54
    about, and you hear a name like getafix, ??
  • 3:54 - 3:59
    (00:03:57:07). You know oblix, asterix. It
    need not be
  • 3:59 - 4:05
    a name, it could be a number. Well, now
  • 4:05 - 4:12
    what? Push the limits. Oops. So now if you
  • 4:13 - 4:15
    look at it a little logically, the radix as
  • 4:15 - 4:18
    a portion ?? (00:04:15:18) is supported only
    in thirty-six,
  • 4:18 - 4:21
    because you have twenty-six alphabets and
    ten digits. So
  • 4:21 - 4:22
    it can go only up to a radix of
  • 4:22 - 4:26
    thirty-six. So if there are some innovators
    out there,
  • 4:26 - 4:30
    who want to actually get a new alphabet into
  • 4:30 - 4:33
    the English dictionary, we could probably
    have a radix
  • 4:33 - 4:40
    of thirty-seven. OK. Let's move on to the
    star.
  • 4:41 - 4:44
    Not the rock star, but the star operator.
    And
  • 4:44 - 4:46
    to start with, let's see what it has to
  • 4:46 - 4:50
    do with Splat Expander. Oh boy, we see more
  • 4:50 - 4:54
    and more of Ruby code right now. So what
  • 4:54 - 4:58
    do you think is name and occupation here?
    As
  • 4:58 - 5:01
    we can see we have a struct which takes,
  • 5:01 - 5:05
    which has two somethings, name and occupation,
    and I
  • 5:05 - 5:10
    create an object off the struct. Now what
    do
  • 5:10 - 5:15
    we see? Nothing fancy. We're still good. Right?
    So
  • 5:15 - 5:18
    the Splat Expander is actually taking my arguments
    and
  • 5:18 - 5:21
    given me the right name and the right occupation.
  • 5:21 - 5:22
    But we don't do things like this, do we?
  • 5:22 - 5:26
    We usually - we are Rails programmers. We
    work
  • 5:26 - 5:28
    in Rails, so we usually have stuff like this.
  • 5:28 - 5:32
    We use keyword arguments. We all have keyword
    arguments
  • 5:32 - 5:35
    to initiate a class like this and stuff. What
  • 5:35 - 5:42
    is the output now? It's Optimus Prime, so,
    the
  • 5:43 - 5:48
    experts? What is name and occupation? Here
    it's changed.
  • 5:48 - 5:52
    That's because the struct always has an array
    of
  • 5:52 - 5:55
    arguments. It does not differentiate between
    this. This will
  • 5:55 - 5:59
    not work on a class. Well, let's get to
  • 5:59 - 6:04
    something more funny. What about this? How
    many here
  • 6:04 - 6:08
    have used this before? We know how to convert
  • 6:08 - 6:10
    an array into hash. How many of you have
  • 6:10 - 6:14
    used this notation before? All right-y, I
    have two
  • 6:14 - 6:18
    people in the audience. Excellent. What happens
    now? What
  • 6:18 - 6:21
    does this do? It actually converts to a hash.
  • 6:21 - 6:24
    And out of curiosity what if I add a
  • 6:24 - 6:30
    seventh element in the array? Error. No like,
    dude,
  • 6:30 - 6:31
    I don't know what to do, what should I
  • 6:31 - 6:34
    do with the last one? Well, let's try some
  • 6:34 - 6:38
    more stars. Who all thinks that the output
    of
  • 6:38 - 6:43
    this is gonna be three, six, and nine? Who
  • 6:43 - 6:50
    all think otherwise? What's the output? Awesome.
    And now?
  • 6:52 - 6:59
    That was easy wasn't it? Now what? String
    ??
  • 7:03 - 7:08
    (00:07:04:24). That's all actually works.
    And let's take it
  • 7:08 - 7:11
    to the next level. How many here know about
  • 7:11 - 7:15
    Stabby, Stabby proc? Here is a sample. So
    this
  • 7:15 - 7:17
    is a Stabby proc that we have which takes
  • 7:17 - 7:22
    - note we are still working with star. So
  • 7:22 - 7:24
    if I make an invocation ?? (00:07:23:07) to
    the
  • 7:24 - 7:31
    block like this, what is the output? Mumbling,
    mumbling,
  • 7:33 - 7:40
    mumbling. It's five. And now you say, I knew
  • 7:40 - 7:42
    that. Right. So basically the short form for
    the
  • 7:42 - 7:46
    first, second last, last, and the rest of
    the
  • 7:46 - 7:49
    stuff is in the middle. Pretty helpful thing
    to
  • 7:49 - 7:52
    know. This works on all. It not, it's just
  • 7:52 - 7:54
    not worked with the Stabby proc, it'll work
    with
  • 7:54 - 7:58
    Lambda, it'll work with a method, too. But,
    as
  • 7:58 - 8:01
    with Ruby, and, if I'm supposed to be standing
  • 8:01 - 8:04
    here and I'm supposed to be lecturing you
    on
  • 8:04 - 8:07
    all this, I'm supposed to know my stuff. Lo
  • 8:07 - 8:11
    and behold, when I was trying these experiments,
    I
  • 8:11 - 8:14
    learned something new. What do you think is
    the
  • 8:14 - 8:19
    output of that? That actually works, and I
    have
  • 8:19 - 8:22
    freaking no idea how. So, well let's put in
  • 8:22 - 8:25
    the slide, and if I have somebody looking
    explain
  • 8:25 - 8:27
    to me how does that dot notation just work?
  • 8:27 - 8:29
    And it works only on this. It will not
  • 8:29 - 8:32
    work on a method obviously, right. But it
    works!
  • 8:32 - 8:39
    So, welcome to the weirdness of Ruby. Well.
    Getting
  • 8:39 - 8:44
    onto really complex stuff. Case statements.
    How many of
  • 8:44 - 8:46
    you have - you all have used cases, right?
  • 8:46 - 8:47
    Case when and stuff like that. Why do you
  • 8:47 - 8:50
    think this works? And I've tried to make the
  • 8:50 - 8:55
    example as complex as possible. All right.
    If Optimus
  • 8:55 - 8:57
    Prime doesn't do this who the hell else would,
  • 8:57 - 9:04
    right? So what is the output? It's pretty
    obvious
  • 9:04 - 9:06
    that we have a multiple of three, because
    nine
  • 9:06 - 9:08
    is a multiple of three, and yeah, because
    we
  • 9:08 - 9:10
    can all read in English and Ruby gives us
  • 9:10 - 9:15
    readable code. We know the output here, right?
    Well
  • 9:15 - 9:18
    it's true. You're absolutely right. Ruby's
    not that weird.
  • 9:18 - 9:20
    It'll give you the right answers. But my question
  • 9:20 - 9:26
    is: multiple_of is a method, right? It takes
    one
  • 9:26 - 9:30
    parameter, which I have passed. How was it
    compared
  • 9:30 - 9:37
    with nine? Where did nine come into the picture?
  • 9:40 - 9:44
    Behind every case, behind every successful
    case, is a
  • 9:44 - 9:47
    case equality operator. So what actually happens
    is that
  • 9:47 - 9:50
    that number nine is used with a case equality
  • 9:50 - 9:55
    operator. And that case equality operator
    is an alias
  • 9:55 - 9:58
    to the proc call, which actually gets us our
  • 9:58 - 10:03
    output. Well. Though this seems pretty straightforward,
    it has
  • 10:03 - 10:09
    just given me immense power. I can now manipulate
  • 10:09 - 10:11
    any way that I want a case equality operator
  • 10:11 - 10:15
    to work. Simply by over riding the equal to
  • 10:15 - 10:16
    equal to equal to method - the case equality
  • 10:16 - 10:19
    operator. And I am good to have any sort
  • 10:19 - 10:22
    of case comparisons that I want to make. I
  • 10:22 - 10:24
    don't care what happens, but it's my call
    anymore,
  • 10:24 - 10:28
    right. Speaking of case equality operator,
    let's go to
  • 10:28 - 10:30
    equality. How many of you all know these symbols?
  • 10:30 - 10:34
    There are operators, an eql question mark,
    and equal
  • 10:34 - 10:41
    question mark. Let's have some fun. Any takers?
    Who
  • 10:43 - 10:46
    all think the output is true? Who all think
  • 10:46 - 10:50
    the output is false? What happened to the
    rest
  • 10:50 - 10:55
    of the people? Don't care. Don't care. That's
    bad
  • 10:55 - 10:57
    for a conference. You know we just got a
  • 10:57 - 10:58
    huge lecture about participation! Again, who
    all thinks it's
  • 10:58 - 11:05
    true? Who all think it's false? Why? Oh, come
  • 11:05 - 11:09
    on man, Ruby's gonna be really neat. Of course
  • 11:09 - 11:15
    it's true. Well, what do you think now? Anybody
  • 11:15 - 11:19
    think it's true? All right man. Stand up dude.
  • 11:19 - 11:22
    Please give him a welcome, because he's got
    it
  • 11:22 - 11:27
    absolutely right. What the hell happened to
    the rest?
  • 11:27 - 11:29
    Dude, we just saw the slide before about case
  • 11:29 - 11:32
    equality. Are you telling me if you use that
  • 11:32 - 11:35
    in a when statement it's not gonna work? Of
  • 11:35 - 11:40
    course it's gonna work. You're just comparing
    to one.
  • 11:40 - 11:46
    And... now what? True? How many people think
    it's
  • 11:46 - 11:52
    true? How many people think it's false? Why?
    So
  • 11:52 - 11:54
    people called false but thought it was true
    but
  • 11:54 - 11:57
    didn't want to answer. Well it's false, and
    I've
  • 11:57 - 11:59
    got you guys so motivated you don't want to
  • 11:59 - 12:03
    answer, right. Damn it. But this is actually
    equality
  • 12:03 - 12:06
    by value. So the value returned by the object
  • 12:06 - 12:09
    is one, the value returned by the float is
  • 12:09 - 12:14
    a floating point one. It's completely different.
    Now what?
  • 12:14 - 12:18
    Now it's interesting. Why is it false? Somebody
    said
  • 12:18 - 12:25
    very confidently, why is it false? Absolutely.
    Now here's
  • 12:25 - 12:29
    the weirdness. This stuff actually compares
    the object ID
  • 12:29 - 12:31
    of the two, and a integer and a float
  • 12:31 - 12:35
    would have two different object IDs. It's
    obvious, right
  • 12:35 - 12:40
    fellas? So. Aiy aiy aiy, true false, what's
    it
  • 12:40 - 12:47
    guys? There string 'a' and string 'a' are
    two
  • 12:47 - 12:52
    different ruby objects. If I switch that two
    a
  • 12:52 - 12:55
    symbol - thank you that's where you go. So
  • 12:55 - 13:01
    with that, let's hit the jackpot. How many
    of
  • 13:01 - 13:03
    you all know what curry is? It's a lot
  • 13:03 - 13:06
    of fun, a lot of fun. But to make
  • 13:06 - 13:10
    it funnier, I actually decided let's have
    the slot
  • 13:10 - 13:14
    machine get three pulls. And I wrote code
    for
  • 13:14 - 13:17
    it on my flight here, so I have no
  • 13:17 - 13:20
    idea whether that's correct or wrong, but
    like, we
  • 13:20 - 13:24
    know it seems a little weird though. I've
    tried
  • 13:24 - 13:27
    to compare if all the three pulls are equal
  • 13:27 - 13:32
    and then I'm preparing some recipe with curry,
    and
  • 13:32 - 13:37
    do you think it would work? It just looks
  • 13:37 - 13:41
    a little bit of weird code, but I shall
  • 13:41 - 13:47
    explain. So curry is a method in the proc
  • 13:47 - 13:52
    class which actually returns a lambda if all
    the
  • 13:52 - 13:55
    parameters are not fulfilled. So in this particular
    case,
  • 13:55 - 13:58
    I needed three parameters, X, Y, and Z, and
  • 13:58 - 14:01
    in the first statement, I passed only one.
    Note,
  • 14:01 - 14:04
    the invocation is in square brackets. It's
    different. And
  • 14:04 - 14:07
    it returns me a lambda, but other times when
  • 14:07 - 14:09
    all the parameters are actually fulfilled
    in the second
  • 14:09 - 14:13
    statement, it actually evaluates the proc.
    Now if you
  • 14:13 - 14:16
    had to write the same code you would have
  • 14:16 - 14:18
    taken different types of input, weighted for
    the user
  • 14:18 - 14:23
    with (00:14:18:29) ...?? one, two, three,
    and evaluated it.
  • 14:23 - 14:25
    You don't need to do that. And these things
  • 14:25 - 14:32
    can actually be very helpful. So curry away.
    So!
  • 14:34 - 14:41
    So you think you can tell protected from private.
  • 14:42 - 14:44
    OK, I am not a - I'm a better
  • 14:44 - 14:50
    programmer than a singer. Right. Private methods.
    All of
  • 14:50 - 14:54
    us have been taught since school, unfortunately,
    that private
  • 14:54 - 14:59
    methods are not inherited. Are they inherited?
    As you
  • 14:59 - 15:01
    can see, foo is a private method. And I
  • 15:01 - 15:03
    ran out of creativity, so I just went to
  • 15:03 - 15:06
    the standard base type foo, blah blah blah
    here.
  • 15:06 - 15:11
    Is this going to work? All of us take
  • 15:11 - 15:13
    a lot of things for granted. How many of
  • 15:13 - 15:14
    you all knew this already, that private methods
    are
  • 15:14 - 15:18
    inherited in Ruby? All right, OK, now I'm
    scared.
  • 15:18 - 15:21
    How many of you all didn't know this? Don't
  • 15:21 - 15:25
    be shy, don't be shy. OK, so this is
  • 15:25 - 15:28
    actually one of the basic principles of where
    Ruby
  • 15:28 - 15:32
    actually breaks traditional object oriented
    concepts. All private methods
  • 15:32 - 15:37
    are inherited. What? How many of you all have
  • 15:37 - 15:42
    seen this code before? Forget the Mongoid::Document
    but include?
  • 15:42 - 15:46
    Wait, come on. Everybody knows it! What is
    include?
  • 15:46 - 15:53
    Is it a keyword? How many thought it was
  • 15:53 - 15:55
    a keyword? Oh it's like that, require or that
  • 15:55 - 16:02
    import in Java. Include is a private instance
    method
  • 16:02 - 16:08
    defined in the class module. So while that
    is
  • 16:08 - 16:11
    sinking in, it raises a basic question. All
    private
  • 16:11 - 16:18
    methods are inherited. So what are protected
    methods? So
  • 16:18 - 16:20
    what about protected methods? So what's the
    difference between
  • 16:20 - 16:23
    private methods and protected methods now?
    We were told
  • 16:23 - 16:27
    in our school that protected methods are inherited
    but
  • 16:27 - 16:32
    not publicly accessible, right. Now private
    methods are inherited
  • 16:32 - 16:37
    but not publicly accessible, right? So what
    are private
  • 16:37 - 16:41
    metho- protected methods? Contrary to popular
    belief, in Ruby
  • 16:41 - 16:46
    protected methods actually work with objects
    and not classes.
  • 16:46 - 16:50
    And you can invoke a protected method on another
  • 16:50 - 16:54
    object within the same lineage. And I say,
    what
  • 16:54 - 17:01
    the... what? Better explained with an example.
    Simple example,
  • 17:02 - 17:03
    for the sake of brevity I put the initializer
  • 17:03 - 17:09
    in one line. What is the output, people? Don't
  • 17:09 - 17:15
    be shy. You have bumblebee to help you out.
  • 17:15 - 17:20
    No takers. Come on man, that's like pretty
    obvious
  • 17:20 - 17:23
    right. You can't call a protected method on
    an
  • 17:23 - 17:30
    object, right? Simple stuff. However. Now
    beginners, so-called beginners
  • 17:31 - 17:33
    can tune out. This is fun. I added a
  • 17:33 - 17:39
    method called fights. Same piece of code.
    What is
  • 17:39 - 17:44
    the output? Take a wild guess, man. Go for
  • 17:44 - 17:51
    it. So either Megatron or Decepticon. Any
    other deceptive
  • 17:52 - 17:59
    ideas? Guys, very practically, a quick lesson
    in human
  • 18:00 - 18:03
    psychology. If that didn't work, would I have
    that
  • 18:03 - 18:08
    slide up there again? It works, man. It works.
  • 18:08 - 18:10
    But why did it work and how did it
  • 18:10 - 18:13
    work? The interesting part is that piece of
    line,
  • 18:13 - 18:16
    earlier in the previous slide, prime dot nick
    did
  • 18:16 - 18:19
    not work, but here target dot nick actually
    works.
  • 18:19 - 18:22
    I have called a protected method on an object.
  • 18:22 - 18:26
    Now that same lineage means that because I'm
    inside
  • 18:26 - 18:29
    the class Autobot, because I'm calling a method
    on
  • 18:29 - 18:32
    the prime object, which is in, which is in
  • 18:32 - 18:36
    Autobot class, and since another object called
    Megatron there
  • 18:36 - 18:41
    is of the same lineage, it's also an Autobot.
  • 18:41 - 18:47
    I can call the protected methods. So why didn't
  • 18:47 - 18:50
    it work earlier? Because everything in Ruby
    is an
  • 18:50 - 18:52
    object. If you're trying to start prime dot
    nick
  • 18:52 - 18:55
    and the previous case was outside the scope.
    So
  • 18:55 - 18:58
    it was effectively in some main class, not
    the
  • 18:58 - 19:01
    same lineage, so it doesn't work. So Ruby
    actually
  • 19:01 - 19:04
    works in the traditional way, but has different
    internal
  • 19:04 - 19:07
    meanings. And if now this was getting a little
  • 19:07 - 19:10
    complicated, how many of you all thought there
    are
  • 19:10 - 19:12
    keywords in Ruby? Cause that man there is
    gonna
  • 19:12 - 19:14
    come and shoot you if you think Ruby has
  • 19:14 - 19:21
    keywords. Does that code work? Is it even
    valid?
  • 19:24 - 19:30
    Should it give me a syntax error? If, how
  • 19:30 - 19:31
    many of you think it will give me an
  • 19:31 - 19:33
    error? Syntax error too? What's wrong with
    you? Have
  • 19:33 - 19:37
    you gone mad?-kind of errors. Any takers for
    syntax
  • 19:37 - 19:40
    errors? Probably not. But if not, then I want
  • 19:40 - 19:41
    to ask you know what is the output of
  • 19:41 - 19:48
    this code? Seriously. There it is, seriously.
    Question is,
  • 19:50 - 19:52
    how many of you all thought this is an
  • 19:52 - 19:55
    error called stack_too_deep. Cause when I
    call false it
  • 19:55 - 19:58
    calls true, once you call true it will call
  • 19:58 - 20:02
    false. And it goes into a recursive loop and
  • 20:02 - 20:08
    case stack_too_deep. Any takers for stack_too_deep?
    Higher, higher! And
  • 20:08 - 20:12
    how many think it will actually work? All
    right.
  • 20:12 - 20:16
    The rest of the people, you all are beginners.
  • 20:16 - 20:17
    But yeah this works. So it does not stack_too_deep.
  • 20:17 - 20:22
    This actually works because true, true's resolution
    is immediately
  • 20:22 - 20:25
    determined as the boolean value. However if
    I had
  • 20:25 - 20:30
    changed these two particular statements, the
    content in the
  • 20:30 - 20:32
    methods to self dot false and self dot true
  • 20:32 - 20:39
    I would probably get stack_too_deep. Not probably,
    definitely get
  • 20:39 - 20:42
    stack_too_deep. OK. So. If this wasn't complex
    enough for
  • 20:42 - 20:49
    you, let's go to modules, the mysterious modules.
    You
  • 20:49 - 20:51
    know what the bull I have with my good
  • 20:51 - 20:55
    friend module, which has power, Megatron is
    super powerful,
  • 20:55 - 20:58
    and he's a force, he's evil. And I want
  • 20:58 - 21:01
    to put that in my class Hanuman. note the
  • 21:01 - 21:04
    star, because that's true. I want the power
    of
  • 21:04 - 21:11
    Megatron with Hanuman. Hanuman! Unfortunately
    this will not go
  • 21:11 - 21:13
    down too well religiously for us, because
    though it
  • 21:13 - 21:16
    will be Hanuman will be as powerful as Megatron,
  • 21:16 - 21:20
    Hanuman is now evil! So what do I do?
  • 21:20 - 21:23
    Is there a way I can cherry-pick from Megatron,
  • 21:23 - 21:26
    saying, I want the power of Megatron but I
  • 21:26 - 21:33
    don't want to be evil? What do I do?
  • 21:33 - 21:36
    I require. Note, I have not included Megatron,
    I
  • 21:36 - 21:40
    require the Megatron method- Megatron file,
    Megatron RB. I
  • 21:40 - 21:43
    define a method called power and I go to
  • 21:43 - 21:46
    the module, tell him to give me that instance
  • 21:46 - 21:50
    method called power, which gives me something
    called an
  • 21:50 - 21:54
    unbounded method. And then I bind it to me,
  • 21:54 - 22:00
    that is self, and I call it. And that
  • 22:00 - 22:07
    makes my Hanuman religiously acceptable to
    society. Question? No.
  • 22:09 - 22:12
    So note self, self is the object call. So
  • 22:12 - 22:14
    it's not working on the class call, it's actually
  • 22:14 - 22:16
    working on the instance. So every object,
    every instance
  • 22:16 - 22:22
    of Hanuman, will actually get the power of
    Megatron.
  • 22:22 - 22:27
    And with that I end my talk and thank
  • 22:27 - 22:30
    my two assistants here. I have, I'm open for
  • 22:30 - 22:34
    a few questions. Do I have the time, guys?
  • 22:34 - 22:36
    Satish, do I have some time? For questions?
    Yay!
  • 22:36 - 22:41
    We have a lot of time for questions! All
  • 22:41 - 22:48
    right, you. Thank you. You have a question
    there.
  • 22:49 - 22:53
    QUESTION: What exactly lead you to title this
    talk
  • 22:53 - 22:57
    as the Dark Side of Ruby? G.R.: Oooh. Now
  • 22:57 - 23:00
    he killed me, didn't he? So that was a,
  • 23:00 - 23:02
    there's a two-sided answer to that. One was
    to
  • 23:02 - 23:06
    fool the organizers, to get the CF?? (00:23:05:23)
    accepted.
  • 23:06 - 23:11
    But more importantly, the dark side of Ruby
    tries
  • 23:11 - 23:12
    to bring out the weirdness in Ruby, which
    is
  • 23:12 - 23:16
    not the evil part, but the hidden part. Like
  • 23:16 - 23:18
    the dark side of the moon where we have
  • 23:18 - 23:20
    all these kind of stuff that breaks, the gotchas,
  • 23:20 - 23:23
    the moment that we feel that are going to
  • 23:23 - 23:26
    help but you don't really need to care about
  • 23:26 - 23:28
    it, but it's really, really important that
    you know
  • 23:28 - 23:33
    these things exist. That's why Dark Side of
    Ruby.
  • 23:33 - 23:38
    So I still love Ruby - no offense. Any
  • 23:38 - 23:45
    other questions? Yes sir. QUESTION: So have
    you suffered
  • 23:45 - 23:47
    because of any of these gotchas in you work?
  • 23:47 - 23:51
    G.R.: Absolutely! This is all part of experience.
    Part
  • 23:51 - 23:56
    (00:23:53:00 ??) is part of experience, part
    is training,
  • 23:56 - 23:59
    and partly is preparation for my talk, trying
    to
  • 23:59 - 24:02
    find out these terms, and figuring things
    out. There's
  • 24:02 - 24:04
    a lot of stuff that I've actually kept out,
  • 24:04 - 24:08
    like you know ?? (00:24:07:03) games. Lots
    of fun
  • 24:08 - 24:11
    with method missing. I haven't even touched
    upon blocks
  • 24:11 - 24:13
    and ?? There's lots of funny stuff which happens
  • 24:13 - 24:17
    there. But I have only half an hour. So
  • 24:17 - 24:18
    the question to you guys also is that in
  • 24:18 - 24:20
    case you find some of these funny things which
  • 24:20 - 24:23
    happen in Ruby, and are able to explain it
  • 24:23 - 24:25
    or not able to explain it, send me a
  • 24:25 - 24:28
    Tweet, send me an email, and I will probably
  • 24:28 - 24:31
    investigate and improve my talk at your expense.
    QUESTION:
  • 24:31 - 24:35
    Yeah. So the other question is, are there
    some
  • 24:35 - 24:40
    practices, certain things that I follow, so
    that I
  • 24:40 - 24:43
    don't get, I don't encounter these? G.R.:
    Well, so
  • 24:43 - 24:44
    one thing, these aren't problems. What I wanted
    to
  • 24:44 - 24:46
    point out here is that this is not a
  • 24:46 - 24:47
    bad part of Ruby, but these are the things
  • 24:47 - 24:50
    that we take for granted. So knowing about
    protected
  • 24:50 - 24:55
    and private being different is important.
    But not mandatory
  • 24:55 - 24:58
    for us to work as Rails programmers. But if
  • 24:58 - 25:02
    you know that, how protected and private actually
    work,
  • 25:02 - 25:05
    it makes us better programmers. So best practices
    of
  • 25:05 - 25:07
    Ruby are already there. You know there are
    things
  • 25:07 - 25:11
    like flip-flops in Ruby which is for lack
    of
  • 25:11 - 25:15
    a better term, a mind star star star star.
  • 25:15 - 25:18
    But, I do not recommend it. But it's there.
  • 25:18 - 25:21
    There are plenty of other things in Ruby,
    too.
  • 25:21 - 25:25
    Which work, which should work, which you should
    use.
  • 25:25 - 25:27
    The bottom line is that Ruby gives us the
  • 25:27 - 25:30
    maximum flexibility to build the way we want
    to
  • 25:30 - 25:33
    build our code. So with great power comes
    great
  • 25:33 - 25:36
    responsibility. And sometimes it's irresponsible.
    So it worked out
  • 25:36 - 25:39
    great. Yeah. QUESTION: So is there a good
    parts
  • 25:39 - 25:41
    of Ruby, like good parts of Java- G.R.: What?
  • 25:41 - 25:44
    QUESTION: Good parts of Ruby? G.R.: Oh, good
    parts?
  • 25:44 - 25:46
    Everything! I've been working in Ruby for
    six years
  • 25:46 - 25:48
    and I still love it. QUESTION: No, there is
  • 25:48 - 25:49
    a famous book called Good Parts of JavaScript,
    a
  • 25:49 - 25:52
    very thin book that allows a subset of language
  • 25:52 - 25:56
    that is very safe to use, that you can
  • 25:56 - 26:02
    be very productive in. So do you, have you
  • 26:02 - 26:02
    come across something that says, like, there's
    a subset
  • 26:02 - 26:02
    that you should stick to and use? G.R.: So
  • 26:02 - 26:05
    the best part about Ruby that I like is
  • 26:05 - 26:09
    the closures, which is completely misunderstood,
    because every time
  • 26:09 - 26:11
    I talk to any person who's getting into Ruby
  • 26:11 - 26:13
    and I show them what a closure is they're
  • 26:13 - 26:15
    like yeah, that's a loop. But it's not. It's
  • 26:15 - 26:18
    like far more better than that. And it's far
  • 26:18 - 26:21
    more - it's more intuitive. It gets into the
  • 26:21 - 26:23
    details and gives us so much more incentive
    about
  • 26:23 - 26:26
    learning how Ruby internals work, that we
    tend to
  • 26:26 - 26:29
    actually ignore it in Rails. And I only (00:26:30:29)
  • 26:29 - 26:32
    have a lot of respect for people who have
  • 26:32 - 26:33
    learned Ruby and gone to Rails, because they
    know
  • 26:33 - 26:36
    the right thing, other than most of us, including
  • 26:36 - 26:38
    me, who got into Rails and then fell in
  • 26:38 - 26:42
    love with Ruby. So it's different. QUESTION:
    So is
  • 26:42 - 26:46
    Ruby the good parts the next book? G.R.: Could
  • 26:46 - 26:48
    be. But I'll need a lot of community support
  • 26:48 - 26:55
    for that. Yes? QUESTION: Yeah, hi, thanks
    for the
  • 26:55 - 26:57
    talk. Just (00:26:55:09). I have been checking
    out Exorcism
  • 26:57 - 26:59
    dot IO. I don't know if you guys have
  • 26:59 - 27:02
    heard of it. So it's actually started by Katrina,
  • 27:02 - 27:04
    who's part of the Ruby Rogues, and I have
  • 27:04 - 27:07
    had a wonderful time running stuff from Ruby,
    like
  • 27:07 - 27:09
    I have been working Ruby for ?? (00:27:08:20)
    for
  • 27:09 - 27:12
    years and also gotchas- G.R.: So, Exorcism
    dot IO.
  • 27:12 - 27:15
    That's great. QUESTION: Like really good Ruby
    idioms that
  • 27:15 - 27:16
    like so it's basically what happens is you,
    there
  • 27:16 - 27:17
    are around ten languages if I'm not wrong.
    You
  • 27:17 - 27:21
    can pick Ruby, you can sub in your solutions.
  • 27:21 - 27:24
    People can look at your code. They can give
  • 27:24 - 27:27
    good reviews. You can help out others with
    your
  • 27:27 - 27:30
    suggestions. Yeah- G.R.: But to add to that,
    there
  • 27:30 - 27:32
    is also some interesting - I'm not sure if
  • 27:32 - 27:36
    you all have heard of Winbgall (00:27:33:08).
    Wingolf was
  • 27:36 - 27:39
    a gem and a simple tool where you can
  • 27:39 - 27:42
    report a problem statement, and using whim,
    you know
  • 27:42 - 27:46
    execute that in the least number of key presses.
  • 27:46 - 27:49
    Similarly there are a few initiatives for
    Ruby golf,
  • 27:49 - 27:51
    where you can actually try things like, the
    beginner
  • 27:51 - 27:54
    ?? they actually said they have things with
    ??
  • 27:54 - 27:58
    you know stuff like, write this piece of code
  • 27:58 - 28:01
    without using arrays, or without using the
    equality operator.
  • 28:01 - 28:04
    And then you start, your mind starts churning,
    and
  • 28:04 - 28:05
    then you dig deeper into Ruby and find other
  • 28:05 - 28:07
    things. So the best way to find these things
  • 28:07 - 28:11
    is to try Ruby ?? All right we had
  • 28:11 - 28:13
    a question there too. QUESTION: Not really
    a question,
  • 28:13 - 28:16
    but just a suggestion, actually, to folks
    who are
  • 28:16 - 28:19
    trying to learn Ruby. I like that he pointed
  • 28:19 - 28:21
    out that some of the things that we've learned
  • 28:21 - 28:24
    in college, classical logic and stuff doesn't
    really apply.
  • 28:24 - 28:28
    What really helped me understand differences
    was that treat
  • 28:28 - 28:31
    Ruby as a object-focused language and most
    of the
  • 28:31 - 28:33
    languages that you probably end up working
    with, Java,
  • 28:33 - 28:36
    C sharp, they'll be class focused. So concepts
    like
  • 28:36 - 28:39
    private, protected, just like you mentioned,
    are all object-level
  • 28:39 - 28:42
    concepts in Ruby. In fact if you wanted to
  • 28:42 - 28:45
    draw parallels again JavaScript is a good
    parallel because
  • 28:45 - 28:47
    JavaScript is also an object-focused language.
    So there are a
  • 28:47 - 28:49
    lot of parallels between JavaScript and Ruby, and
  • 28:49 - 28:52
    less between Java and Ruby.
  • 28:52 - 28:53
    QUERANT: Yeah, and-
  • 28:53 - 28:56
    It's like the dot, I'm sorry, that dot thing just amazed me.
  • 28:56 - 28:59
    ??, just having a dot.
  • 28:59 - 29:01
    and I was actually kind of disappointed when it
  • 29:01 - 29:04
    ended. I just wanted more. OK.
  • 29:04 - 29:05
    G.R.: Cool. Thank you.
  • 29:08 - 29:09
    Yes?
  • 29:11 - 29:13
    The Well-Grounded Rubyist. A must read.
  • 29:14 - 29:16
    V.O.: Somebody Tweet about it, please.
  • 29:16 - 29:18
    V.O.: We'll take one last question.
  • 29:20 - 29:22
    QUERANT: How does it help to have protected and private method
  • 29:22 - 29:25
    different from other languages? The way Ruby does it?
  • 29:25 - 29:28
    G.R.: I'm sorry. Did you ask what is different between protected and private-
  • 29:28 - 29:30
    QUERANT: No. How does it help to have a different design implementation,
  • 29:30 - 29:31
    to have it different.
  • 29:31 - 29:34
    G.R.: Oh. Excellent. And I love answering this question.
  • 29:34 - 29:36
    Though sometimes it can be a little long.
  • 29:36 - 29:40
    Why is private and protected different private and protected different in Ruby than
  • 29:40 - 29:42
    in other traditional standard languages?
  • 29:42 - 29:43
    Because Ruby did it right.
  • 29:43 - 29:45
    That's how it's supposed to be.
  • 29:45 - 29:46
    Example.
  • 29:48 - 29:50
    You know, when we talk about object-oriented programming,
  • 29:50 - 29:53
    it's supposed to be about objects. Things that you can see and touch.
  • 29:53 - 29:55
    For example: Inheritance.
  • 29:55 - 29:57
    Right. What is inheritance?
  • 29:57 - 29:59
    In, put it in, no uncertain term, though, it's my
  • 29:59 - 30:01
    ??
  • 30:02 - 30:05
    Right. My inherited land my from my ancestors.
  • 30:06 - 30:09
    It's private. It's inherited.
  • 30:09 - 30:12
    You can. It, it comes within the family.
  • 30:12 - 30:14
    It can be protected. Can be private.
  • 30:14 - 30:17
    But, if you have your dad's wallet, do you inherit it?
  • 30:18 - 30:20
    You can choose to use it. You can choose not to use it.
  • 30:20 - 30:22
    It's just there.
  • 30:22 - 30:25
    The inheritance of land can be protected because it always stays in the family.
  • 30:25 - 30:28
    Like, for example, the money in the family is protected.
  • 30:28 - 30:31
    But that's always dealing with parents and children.
  • 30:31 - 30:35
    You can't go, your father. Just because you have a father and I have a father
  • 30:35 - 30:37
    I can't go asking your father for money.
  • 30:37 - 30:41
    Right. It's immense, how you determine. And of course public is the same everywhere.
  • 30:41 - 30:44
    So Ruby did it right, because it's, everything's object-centric.
  • 30:45 - 30:48
    Exactly what Aman said just now. Everything's object centric.
  • 30:49 - 30:52
    And, traditionally in object-oriented concepts, it started off right
  • 30:52 - 30:57
    and then it went all across the tangent, where you had this inheritance, and
  • 30:57 - 30:59
    classes and stuff like that. All the standard
  • 31:00 - 31:06
    things that I never talk about: incapsulation, abstraction, polymorphism
  • 31:06 - 31:09
    which seem to be the essence of object-oriented programming but is not.
  • 31:10 - 31:12
    V.O.: So, a follow-up question.
  • 31:12 - 31:16
    I have it. So is there a well-known pattern that makes use of, makes good of
  • 31:16 - 31:18
    private and protected?
  • 31:19 - 31:20
    We understand why- G.R.: Good qu-
  • 31:20 - 31:21
    QUERANT: Is it because it
  • 31:21 - 31:24
    G.R.: Include. Include. Include is an excellent example.
  • 31:24 - 31:27
    There are actually other patterns, too.
  • 31:27 - 31:31
    There are other patterns which allow for protected, protected is used very heavily for
  • 31:31 - 31:34
    all sorts of, the usually standard, why use protected, for example
  • 31:34 - 31:37
    protecting your password generation algorithm.
  • 31:37 - 31:39
    Something like that. But that's the deal with objects.
  • 31:39 - 31:42
    And since objects can be called only from their own lineage
  • 31:42 - 31:44
    you're always safe. Good question.
  • 31:46 - 31:47
    All right. V.O.: Anymore questions?
  • 31:48 - 31:50
    If there's anymore questions, we'll take it offline.
  • 31:50 - 31:52
    G.R.: Sure. Thank you so much
  • 31:54 - 31:55
    V.O.: Thanks Gautam.
Title:
Garden City Ruby 2014 - The Dark Side of Ruby by Gautam Rege
Description:

more » « less
Duration:
32:28

English subtitles

Revisions