Weird Ruby
Bozhidar Batsov • Sofia, Bulgaria • Talk

Date: December 07, 2024
Published: December 16, 2024
Announced: unknown

Bozhidar is the author of RuboCop and the editor of the community Ruby
style guide. He's involved in a myriad of open-source projects, mainly
related to Ruby, Clojure, and Emacs. Many would probably describe
him as an Emacs zealot (and they would be right).

This talk is very strange and unusual, unexpected, or not natural. It's a talk about Ruby!

Ruby Banitsa Conf 2024

00:00:06.080 hello everybody uh usually I put a slide greeting people in their native language
00:00:12.559 usually I don't know it at all but today so it happens that I'm a master of
00:00:18.640 Bulgarian uh usually I also have slides explaining my name but I think today
00:00:24.000 this is kind of redundant my friends uh especially those
00:00:29.560 from turn from the high school typically call me bug but you can also call me Bojo
00:00:35.440 because you know Bojo is not a boo uh more boring slides that you
00:00:41.520 probably don't need an important slide usually I look better today however I had to wake up at
00:00:50.399 6:30 a.m. because yesterday I was at uh the Strasburg Christmas Market with a
00:00:56.960 dear friend of mine before this I taught my goddaughter some important life
00:01:03.119 lessons because she's turning 10 this week and uh she's
00:01:08.320 ready uh she was way too ready uh and uh this is why today I have
00:01:14.920 a slight hangover because she's 10 but I am
00:01:21.119 40 and uh yeah uh gady is a dear friend
00:01:26.240 of mine as well this is why I inflicted myself the pain of coming uh from France
00:01:32.240 today uh and you know we have been discussing uh doing something at Ruby
00:01:38.640 banit for a very long time and I think I never came that's what she
00:01:44.719 said but anyways uh this year because I'm older and uh maybe wiser I'm here
00:01:53.520 whether I'm Wiser or not is debatable because I did watch the second part of Rebel Moon
00:02:01.719 twice ah not twice uh and I did reread
00:02:07.520 finally after 20 years of suffering The Wheel of Time this was a
00:02:13.440 big mistake uh uh I knew I would regret it uh but there is something uh deeply
00:02:20.120 broken um in me anyways I told you I have some drinking issues gady told me not to
00:02:27.519 discuss drinking issues on the conference it's fine um and here is a snapshot of
00:02:35.400 my interests in life uh this is my Instagram you see that uh I like uh I
00:02:41.360 like watches I like o I'm not sure who this guy is I know
00:02:49.480 it's talk man it's talk I have no idea who he
00:02:55.560 is yeah but uh you know this is the thing about being in program pramming in your 40s at 20 you care about being a
00:03:05.599 better programmer you care about the craft uh about design patterns uh idioms
00:03:12.480 uh best practices at 30 you realize this is kind of pointless maybe you decide
00:03:18.760 that you want to try to be a manager and bring more impact uh Through Your Divine
00:03:24.159 leadership style at 40 you realize that the only important thing in life are
00:03:29.799 mean watches true story I'm uh really
00:03:36.080 obsessed like I watch Apocalypse Now and uh all I'm thinking is damn uh Captain
00:03:44.280 ward has a really good example of Captain Ward this is the Seiko
00:03:50.360 6105 a legendary dive watch the second dive watch to be produced by
00:03:55.760 Seiko and um the film makers really Led Watch is because you can see that kinel
00:04:02.439 CTS Maron Brando wears a Rolex which is the arch nemesis of
00:04:08.000 Seiko this is the GMT Master 2 I think so a very evil watch and why am I saying
00:04:16.120 this because Ruby originates in Japan Seiko also originates in Japan and uh by
00:04:23.840 trying a lot of watches I have scientifically concluded that I am 20% more program uh productive when I'm
00:04:30.960 wearing a SEO like this one uh it makes me a better presenter as well I have
00:04:36.639 also concluded that wearing a Casio makes me 30% more productive but I
00:04:42.039 didn't have any Casio with me today so this is not going to be my best talk I can uh tell you more about
00:04:48.560 watches uh about uh what they present for your fun level social status uh but
00:04:55.800 uh the only person in the Ruby community that U appreciates uh my jokes about um
00:05:02.320 watches is sadly not here so I'll switch to more boring and more educational
00:05:09.520 topics so yeah uh genady alluded that uh I'm am supposedly known for working on
00:05:16.160 some open source projects uh RoboCop is Alive and Well I
00:05:21.880 know I'm supposed to say something about it uh this year the big news is that we
00:05:27.479 support prism which is nice and rubocop is faster than ever in case
00:05:34.360 it wasn't fast enough for you already so I'd say great
00:05:39.560 success uh another interesting Milestone is that rubocop turned 12 years um in
00:05:47.039 April quite earlier this year and uh next year will be I guess uh pretty
00:05:52.840 critical for the future C birthday uh so yeah lots of lots things
00:06:00.080 to celebrate also before this talk I checked that uh this is uh the total
00:06:05.360 number of downloads so if somebody wants to nudge us over the
00:06:11.319 edge impossible is nothing um this slide I forgot to update
00:06:18.080 so I'll conveniently skip it um but uh yeah it has been a wild
00:06:25.319 right uh during those past 12 years um RoboCop made it uh in the list of um
00:06:33.440 alltime 100 gems in terms of downloads and if you look around it you'll see
00:06:38.840 that most of the gems around it are just troc cops dependencies like progress bar
00:06:44.560 as rainbow so we propelled a lot of gems into into
00:06:51.120 Fame uh you know but there is an actual talk today and uh I think the topic is um
00:06:59.319 kind kind of interesting I want us to talk a little bit about some weird
00:07:06.120 aspects of Ruby uh in case you didn't notice I'm a weird person so I like
00:07:12.840 weird stuff that's why most of my presentations in the past 10 years have started with dictionary definitions
00:07:19.680 because why not uh so if you consult um one
00:07:25.160 dictionary I forgot which one about uh the word weird you'll see that um it can
00:07:32.160 be described as um something of strange or extraordinary
00:07:37.479 character uh something of or relating to or caused by witchcraft or the
00:07:43.960 supernatural this sounds very appropriate when we're talking about programming another dictionary
00:07:50.879 definition very strange and unusual unexpected and uh or not
00:07:57.599 natural so good stuff good stuff here and uh if we um if we try to find
00:08:05.680 something about Ruby itself I guess the best uh way to describe it would be um
00:08:12.120 this blurp from uh Ruby link.org a dynamic open source programming language
00:08:18.319 with a focus on Simplicity and productivity it has an elegant syntax in
00:08:23.720 syntax uh that is natural to read and easy to write I have to tell you I am
00:08:30.039 impressed that I can even speak in uh English in my present state but you
00:08:35.200 probably can't tell this so the important thing before we dive into this talk is that weird is not the same as
00:08:43.440 bad I have a bad Ruby talk as well and it is brutal this is a positive talk
00:08:50.680 weird is good Weir is also subjective um and uh if you ask
00:08:57.000 different people about what aspects of Ruby do they find to be weird you're
00:09:02.160 going to find always different answers I guess I was kind of uh lazy and U I
00:09:10.839 thought let's ask uh the world's foremost expert on rubby what are the 10
00:09:17.200 most weird features so I use this prompt uh and I got the following
00:09:25.000 answers number one monkey patching weird stuff
00:09:30.160 number two open classes number three symbols yeah I was like okay that's
00:09:38.279 weird um not the symbols blocks procs and
00:09:46.200 lambdas number five method missing number six meta
00:09:53.440 programming number seven so this was very funny because chat GPT callede this
00:09:58.920 Flex I syntax it's like okay whatever o your
00:10:04.480 boat number eight implicit returns oh
00:10:11.240 yeah uh this was uh also a bit hard to parse at first self context changes uh
00:10:19.320 but basically this is the fact that self is different in different
00:10:25.160 parts of the code um and uh fin fin number 10 keyword arguments with default
00:10:32.800 Val I was like okay okay so I used to be worried about
00:10:38.320 chat GPT but I think we are going to have jobs for at least two three years more which is a good thing and that's
00:10:45.680 all I have for you today let's go to the
00:10:54.279 restaurant there are 105 slides more
00:10:59.600 and that unfortunately is not a joke my friends so uh Ruby is uh in its
00:11:09.639 managerial age 31 good age good age you can still drink reasonably
00:11:16.839 well and uh you are not afraid to go to a conference after
00:11:22.519 party so uh I'm a big fan of Batman and um I have
00:11:28.959 learned a lot about life for various Batman movies and comic books most importantly
00:11:36.360 I learned from uh The Dark Knight which I watched seven times in one week that
00:11:42.720 whatever doesn't kill you simply simply makes you weirder and uh if you're
00:11:49.720 31 clearly you survived a little bit in the world and you must have become quite
00:11:56.839 quite weird so um I do think that um a lot of the weirdness in Ruby is just a
00:12:03.600 function of its age 31 years uh for a programming language is like 300 years
00:12:09.320 for a human so it's a pretty good age so
00:12:14.880 how is this talk going to proceed uh there are going to
00:12:19.920 be different things that I think are weird and I'm going to be showcasing
00:12:26.839 showcasing them to you I'm going to classify those using three levels of
00:12:33.440 weirdness number one everybody knows what and I will be marking this with the
00:12:40.959 F in emoji nothing surprising the second level which is
00:12:47.720 more weird weirder is going to
00:12:53.800 be okay we're not going to pause this and we are going to fix it this
00:13:00.560 uh or uh as we say in
00:13:06.839 Bulgaria and uh level three in
00:13:12.360 English let's say it's uh holy and in Bulgaria it's going to be
00:13:20.000 z uh and because I wanted to incorporate madona in this presentation you know I
00:13:26.160 have this uh weird friend from sard um who always gets um shocked by some
00:13:34.240 weird stuff and says Madonna uh you I thought this is appropriate thing to do and I don't know
00:13:41.920 maybe a little bit uh less explicit so uh we are going to start
00:13:48.920 with a little bit of calibration I'm going to discuss a few
00:13:54.120 well-known examples of weird functionality in Ruby and we are going
00:13:59.519 to rank it together before before we dive into the weirder stuff so what do I
00:14:06.160 mean by those uh usual things like people often uh uh say that it's
00:14:14.720 kind of weird that in Ruby you can alter the natural uh flow of control meaning
00:14:21.000 that with end and begin blocks you can basically execute this bit of code
00:14:29.440 before the puts and this bit of code after everything else so we are not
00:14:35.880 executing it sequentially um whether you should be using this is
00:14:42.839 highly debatable but it is here for illustrative purposes so how weird do
00:14:49.000 you think this is other langu they yeah yeah I I think
00:14:57.360 this was copied from Pearl so definitely not uh not
00:15:03.000 groundbreaking uh this is the infamous flipflop operator that everybody knows
00:15:08.600 about nobody has used except whoever wrote this snippet that I have seen in
00:15:14.440 10 different books on Ruby maybe it's good also for um those uh programming
00:15:22.040 competitions to write the obfuscated code I forgot the name very popular in Japan
00:15:31.519 uh they had some
00:15:37.800 terminology uh you remember this we were at this uh competition when there
00:15:43.880 were maybe I forgot it but this is um
00:15:49.560 more than one because I think people use this you're the only one with that needs to De with other no I think it's a lot
00:15:56.600 just because everybody knows it yeah but I don't know I think this is uh
00:16:05.000 this is maybe a bit more interesting U you know I hope you know or if you don't
00:16:10.480 know it it's good for you that there is a different way to write comments in Ruby uh so-called begin end comments but
00:16:18.440 uh there is a weird thing about them if you don't place them at the very beginning of a line they don't work uh
00:16:24.800 this is a syntax error yeah I think uh this right uh
00:16:34.199 and this actually works but I would personally think it's broken
00:16:40.920 so yeah I um I also find it very weird that U we still have in
00:16:48.839 Ruby yeah yeah they used to be meaningful in the earlier days of Ruby
00:16:55.639 up to Ruby 1.8 but now there essentially um an
00:17:01.720 alternative way to write a single character string U not very weird but um
00:17:09.520 somewhat weird of course you can also use them uh for U slightly weirder
00:17:18.480 yeah um I I I learned a lot of weird
00:17:26.679 working on rubocop uh I have a attack weird Ruby in the issues
00:17:34.080 and every time I need to update this presentation I just uh scroll through
00:17:39.520 the last 10 20 issues good stuff anyways uh let let's continue calibrating
00:17:46.000 ourselves um re assignable con constants I think that uh the name alone is a
00:17:53.480 strong indication that something is uh very weird here at I hope you're all
00:18:00.840 aware and um you all agree with this we continue class variables I think they're
00:18:08.320 very well known not uh super weird know that's a
00:18:14.760 l and uh we kind of fix them in Ruby 3 this is
00:18:21.520 uh the point of this slide you are going to start getting an error that you
00:18:29.360 wouldn't be getting in the past if you decide to do something that looks
00:18:36.080 dangerous so uh they I'm trying to say that they're not as problematic as before so
00:18:43.240 maybe at some point people are going to start using them more I had um
00:18:49.559 completely uh missed this change in Ruby and I learned about it when um I got a
00:18:55.360 ticket should we unbun uh variables because it seems they're not as
00:19:00.880 dangerous as in the past so you know we keep learning new stuff every
00:19:08.720 day okay so small calibration exercise completed let's start getting more weird
00:19:16.600 I'm going to be showing you examples and uh you are going to be
00:19:22.120 telling me how weird they are so uh what does this bit of code uh return as the
00:19:36.000 result you tell me I'm voting for false any other
00:19:42.600 opinions true false okay any anything
00:19:54.679 um yeah so this can be only true
00:20:00.120 because this is those are the rules of
00:20:06.559 Precedence yes yes uh it's not needed uh
00:20:11.760 at um yeah Master programmer this is not looking good for
00:20:17.720 you but uh this is the same same
00:20:23.360 thing true or false true or false I'm not anymore
00:20:32.480 yes sir this is false because the operators are the same but somebody decided that the rules of Precedence are
00:20:40.400 different basically uh the ERS ERS version and the bar bar or however this
00:20:47.400 is called in English bind uh um the end
00:20:52.880 has a higher precedence than or but um with their English word versions they
00:20:59.000 have the same precedence and they're bind from left to right which uh which
00:21:05.520 results in some very confusing behaviors so for me this is this is a madona
00:21:12.960 moment u i we continue how many of you know what
00:21:21.159 this
00:21:26.480 is it's a it's three letters
00:21:31.520 technically okay
00:21:37.679 so madon um okay so it's basically something that really
00:21:45.400 looks like a hash and has your environment variables
00:21:50.640 there um but um if you check it's class
00:21:56.840 I know it what object you see this a monkey packaged object yes
00:22:08.720 yes I redeemed myself okay you redeemed yourself my friend okay so more quiz
00:22:15.320 questions uh I think this talk is working a lot better with uh people yes
00:22:21.440 yes yes uh what is uh what do you see
00:22:30.080 I know this this is an OCT number
00:22:35.400 Z so what is it in decimal
00:22:42.480 15 he unredeemed
00:22:47.720 himself yeah when I first saw this I felt like this
00:22:53.960 bear uh who the guy the guy who who was sitting on the
00:23:00.799 I was the bear I was the bear I'm a very violent person um you know I used to
00:23:06.080 play a lot of uh Warcraft 3 and I was pretty good at the height of my career I
00:23:11.240 made it to top 50 in the on the European ladder but I broke so many keyboards
00:23:16.720 after this point I realized I can never be a professional gamer so I became a programmer
00:23:24.120 instead yeah yeah I was uh very very V night
00:23:29.760 elf okay so um there is an alternative Syntax for this which I think reads a
00:23:36.799 lot better and um actually it's kind of consistent with the alternative syntax
00:23:44.279 with the not alternative but the standard Syntax for binary and uh for
00:23:50.200 hex decimal so you know I found it weird that they decided to treat um the octal
00:23:59.159 um uh digits um de octal numbers differently and yeah that's Ruby we have
00:24:05.840 weird stuff so tell me will this bit of code compile or
00:24:16.960 not yeah otherwise it wouldn't be
00:24:24.559 here will this bit of code compile oh
00:24:30.120 no do I see a yes I I yes okay let's uh
00:24:36.279 let's do a show of hands how many people think this is going to compile I I will do it okay
00:24:42.640 five six okay so it seems we split
00:24:48.080 evenly no no it wouldn't it wouldn't uh there there is a special handling um for
00:24:55.840 if that doesn't extend to other
00:25:03.039 keywords um no you cannot start with numbers
00:25:08.919 yeah so R from you I expect it more than from
00:25:14.760 genady I haven't drink any okay so um is this a valid bit of
00:25:23.799 cod yes no oh is this will be a valid one because you because of
00:25:30.720 the so this is UN those are two unes so do you really trust this guy after all
00:25:37.520 the mistakes he made this is my vote I I would say this is a valid for me okay and is this
00:25:48.480 valid I would say this is valid to be which
00:25:55.039 probably it is actually valid okay and it's valid for a very simple
00:26:01.480 reason um oh see Redemption uh so any
00:26:07.600 such bit of code essentially evaluates to X and uh this is where um I mentioned
00:26:14.960 that U you know Twitter these days is name Tex and it's and I'm on Blue
00:26:21.159 Sky which is blue and great I'm also on Mastodon at mostly on Blue Sky
00:26:29.080 I expect 20 new followers after the end of This brilliant talk
00:26:37.159 uh make it
00:26:43.880 happen get creative get creative okay we are going to the really good stuff
00:26:49.600 objectoriented negation so um a little known thing in Ruby is that the negation
00:26:56.320 operator is actually a standard method and you can write
00:27:01.799 like this and uh it will actually work not
00:27:07.720 very shocking right but uh you can also um override
00:27:14.279 it and you can calculate factorial in a really sexy way but I definitely don't
00:27:21.600 recommend doing this at home because your code is not going to behave very
00:27:26.840 well if you do it um so other things that I find
00:27:33.279 interesting um I guess everybody knows that um numbers have a natural order to
00:27:40.240 them and um you can generate a range of
00:27:46.200 numbers this is U backed internally by the suck method which doesn't suck but
00:27:54.000 it succeeds it's a very successful method that doesn't suck um and you know it is
00:28:01.760 used internally in ranges as I mentioned but um you can also use suck for
00:28:10.480 Strings and U the results might be somewhat um consistent or surprising
00:28:19.240 depending on the example or this one here okay maybe it's
00:28:26.360 okay here I'm not sure um and I know what um programmers
00:28:31.640 typically are thinking after they see this this is the best way to generate a
00:28:37.279 version a version range in Ruby and they write code like this and it works but
00:28:43.720 then they try it a little bit um more and the results are quite quite
00:28:51.760 surprising so yeah
00:28:59.799 don't do this don't do this uh I also did some um examples um in uh in
00:29:07.039 cic and uh the behavior was kind of weird here I was encouraged okay here I
00:29:14.640 was like I haven't seen this letter in cic yet and I remember when I was doing this
00:29:21.360 talk a bro people you have typos in your slides those letters don't exist
00:29:29.640 but they exist they exist okay more interesting things um
00:29:38.120 so does anybody know what this code
00:29:44.760 does gady is looking to embarrass himself even further it unfreezes the
00:29:55.159 Frozen that's true I is is true and this is how you might uh use
00:30:04.760 this I never needed it in my entire career uh which is 15 years in
00:30:10.840 Ruby but it exists optimizations after this ended up in in
00:30:17.799 Ruby yeah I'm very happy that we we are going this way like seriously like this
00:30:23.519 is going be proper it's good that this is happening in my managerial ears when
00:30:28.679 I don't give a sure uh I don't like this and by the way
00:30:36.919 there is a minus that is freezing strings if I remember correctly yeah but I will show you some
00:30:44.720 uh better things like uh an exercise in
00:30:50.640 inconsistency zero okay non zero NE false what
00:31:04.039 made in Japan my S uh some uh other funny examples about
00:31:13.039 Infinity just as consistent is one finite yes infinite
00:31:28.159 it gets even better uh I'm not sure how many people even know this but uh when
00:31:34.559 you use a her dog by default it behaves like a double quoted string litero so
00:31:41.120 things like interpolation and whatever are going to work there and U we can
00:31:47.600 illustrate this with an example this is of course the opening of
00:31:52.880 The Lord of the Rings and The inscription on the One Ring
00:31:58.880 I uh I would have written this in uh the language of the Orcs 20 years ago but
00:32:05.360 today old mature wise this is the example and the outcome is more or less
00:32:10.760 what You' expect uh we have the interpolation in Mordor okay nothing
00:32:17.240 shocking and you might be wondering is there a way to surpress the interpolation uh the way is slightly
00:32:24.760 weird you have to single quote um only the top
00:32:30.559 delimiter okay and the results are as you will expect them but here it gets
00:32:37.039 interesting if you do this you can actually have spaces in the delimiters
00:32:43.600 and you can actually have everything you want in the delimiters this is valid
00:32:54.000 code uh and uh with here dos uh there is something even less known you can Nest
00:33:02.320 those uh I learned about this from a ruby rubocop issue as usual so I
00:33:08.360 struggled to understand in the beginning what this code was supposed to do uh so
00:33:13.799 I illustrate it with an example and I know a value minus 5 but uh
00:33:25.279 o you can do this if you want I'm not going to hold this against you
00:33:31.919 but in reality I will okay so uh now the last 25 slides
00:33:39.480 and some of my favorite weird aspects in Ruby so how many ways uh are there to
00:33:47.480 invoke a Lambda show off hands for one for
00:33:53.440 two three okay four
00:34:05.679 five okay so um this is our Lambda and let's see um how many
00:34:12.520 different ways we can Co it I guess this is the tried and true
00:34:18.520 one yeah uh this is the modern sexy
00:34:26.040 way this is the old sexy way like geek CH
00:34:35.639 something wait wait for
00:34:43.839 it this can be only no there is
00:34:53.720 no was another weird one I don't make mistakes I don't make mistakes uh composition
00:35:00.720 maybe you're thinking of the composition was something I
00:35:06.720 remember yeah so tell me which method in Ruby's standard library has the most
00:35:13.280 aliases guesses size
00:35:24.640 no no
00:35:35.359 no I I think I'll have to give you a
00:35:40.880 hint so this is the original method which somebody
00:35:46.760 decided to as as member as key and as
00:35:55.119 key um and uh there are a few uh honorable mentions uh that have just two
00:36:03.200 aliases QP has also Q deck and Q shift Q push he has Q ank and Q give it to me
00:36:13.280 select filter find all um I don't
00:36:19.920 know I don't know it's too much and uh we are moving into the
00:36:27.240 Finish in C so do you know that uh if you put
00:36:32.640 something after the end thingy which I wouldn't call um I think it was a
00:36:39.079 compiler pragma or whatever this is actually going to be bound to the data
00:36:47.040 object yeah I believe and you're going to get this beautiful
00:36:53.680 output uh we can also we can also create in Ruby like um in
00:37:00.000 physics I remember how my professor in the Technical University of Sophia taught me that you cannot create energy
00:37:07.280 out of nothing you could only transform it I I showed this code to him
00:37:18.760 nothing I created an empty array an empty string some
00:37:25.160 numbers this guy knows and uh another uh another fun thing uh
00:37:34.240 you can do this in um in a slightly different and typically in the style of
00:37:39.599 Ruby inconsistent way because as you can see there is extended validation when
00:37:45.760 you're converting uh to an integer or a a float NE but for a race and hashes
00:37:53.280 this is cool I don't know I don't know
00:37:58.520 I will uh I will conclude this uh talk quoting um some python wisdom explicit
00:38:06.599 is better than implicit errors should never pass silently in the face of
00:38:12.000 ambiguity refuse the temptation to guess but this is Ruby we don't give a we
00:38:19.640 don't give a we want to keep Ruby weird it was a great pleasure to be here
Explore all talks recorded at Ruby Banitsa Conf 2024