Summarized using AI

Discover Modern Ruby Features

Lucian Ghindă • May 31, 2024 • Verona, Italy • Talk

In the talk "Discover Modern Ruby Features," Lucian Ghindă, a Senior Ruby Developer, explores the evolution of the Ruby programming language and its new features introduced over the past five years, mainly since the release of Ruby 2.7. The session is aimed at encouraging Ruby developers to adopt these modern features to enhance code readability and maintain relevance in the ever-evolving landscape of programming languages.

Key Points Covered:

  • Introduction to the Speaker's Background: Lucian identifies as a polymorphic Ruby developer, having transitioned from a silent user to an active contributor in the Ruby community. He shares his passion for the language and his desire to keep Ruby relevant.
  • Features Highlighted: The talk delves into several new features:
    • Endless Method Definitions (Ruby 3.0): Simplifies method definitions by allowing a method to be defined in one line using def name =, offering clarity in code structure.
    • Data Classes (Ruby 3.2): Streamlines the creation of immutable classes, emphasizing their utility in organizing simple data structures compared to traditional classes and hashes.
    • Hash Literal Value Omission (Ruby 3.1): This feature allows developers to omit values for hash keys when they have the same name as local variables, thus simplifying hash creation and enhancing code conciseness.
    • Argument Forwarding (Ruby 2.7): This provides syntactic shortcuts for passing arguments, reducing code repetition when calling methods.
    • Pattern Matching: Introduced as a significant feature to enhance clarity in handling various data structures, enabling more expressive and maintainable code.
  • The Importance of Experimentation: Lucian urges the community to experiment with new features in real-world coding scenarios and provide feedback to the Ruby committers to foster further evolution of the language.
  • Concerns about Language Evolution: He discusses the challenges mature languages face in maintaining stability while introducing new features to avoid alienating long-time users. He emphasizes the balance between tradition and innovation to keep Ruby vibrant for the next generation of developers.

Conclusion:

  • To ensure the future relevance of Ruby, Lucian encourages developers to embrace new features, experiment with them in production-like environments, and actively participate in discussions about their usefulness. By doing so, the Ruby community can drive the language's evolution in a direction that honors its core values while adapting to contemporary programming needs.

Discover Modern Ruby Features
Lucian Ghindă • May 31, 2024 • Verona, Italy • Talk

Exploring Ruby's evolution as a language: Dive into new features like pattern matching, hash literal value omission, object shapes, and a wide range of new abilities. Learn their real-world applications & impact on code readability in my talk. Let's unlock Ruby's full potential!

Lucian Ghinda is Senior Ruby Developer | Curator of Short Ruby Newsletter.

---

rubyday 2024 is the 11th edition of the Italian Ruby conference, organized by GrUSP,
The event is international, and all sessions will be in English.
📍 Verona | 📅 May 21, 2024

Join the next edition
🔗 www.rubyday.it

---

rubyday is organized by GrUSP.
We organize events, conferences and informal meetings involving Italian and international professionals.
We aim to make the ecosystem of the Italian world of web development better both in terms of skills and opportunities by creating greater awareness through comparison and sharing.

Subscribe to our newsletter:
✉️ [www.grusp.org/en/newsletter](http://www.grusp.org/en/newsletter)

 Follow us
 Website https://www.grusp.org/en/
 LinkedIn https://www.linkedin.com/company/grusp
 Twitter https://twitter.com/grusp
 Instagram https://www.instagram.com/grusp_
 Facebook https://www.facebook.com/GrUSP

rubyday 2024

00:00:06.319 developer uh because I found myself in different roles not exactly maybe I wanted but I just uh fill up the empty
00:00:13.160 space in different teams where I was uh I'm the curator of short Ruby newsletter spoiler it's anything but short if you
00:00:20.600 see it so it's very long newsletter right and because seems like I'm good at emails I'm um what you see today is part
00:00:26.920 of a course that I'm building um where you receive by email a piece of codee and then I deep
00:00:32.800 dive medium length into it let's say um so today I'm I'm going to to to talk in
00:00:40.440 this talk about two main things one I'm going to try to pit you in some way to adop new features I'm not going my my
00:00:47.960 purpose is not to say this is good or this is bad but but more like just taste this and see how it is for you
00:01:09.960 right and then I I pick up some of the things that were added to the language in the last fiveish years since Ruby
00:01:18.400 2.7 um I'm not saying these are the best ones but these are I found them the ones
00:01:23.520 that are maybe changing the shape of the code right so so the the sh how the code
00:01:28.759 looks like and and so um I um I I was a
00:01:33.920 silent user of the Ruby for many years I I think I I started Ruby in 2006 2007
00:01:40.759 something like this and I was a silent consumer but in the last 3 four years I I I wanted to get involved in the Ruby
00:01:48.040 right and I I and I have two main concern one is um one is about how to grow the Ruby Community I'm not a ruby
00:01:54.439 core committer so my my purpose is like I'm a user how can I do something and make it better or bigger or I don't know
00:02:01.360 contribute in some ways and another one is how to keep Ruby relevant for the next 30 years because I love Ruby and
00:02:08.599 and I will see myself using it for many years hopefully right and I love Ruby in
00:02:14.160 two ways I I come to Ruby with a background from java and I was writing some python for some backend servers and
00:02:21.000 some PHP on the side 2006 and I was doing UI interfaces with with something
00:02:27.440 like scriptaculous or prototype. s and there was some other stuff back then
00:02:33.160 right uh prej query for the young people here um and and and I found I I found
00:02:39.480 first Ruby to be like Freedom like I F I felt those languages are are having a lot of constraints and while writing
00:02:46.120 Ruby I feel like oh my God this is freedom to write how I want it in a way right and and also Ruby for me it's it's
00:02:53.360 it's it's about creativity because I feel like I can create something by putting my thoughts into code right in
00:02:59.599 in in in some ways and also because in Ruby we're talking about beautiful codes and and beautiful is very close in an
00:03:05.879 embedding maybe to Art like things right and art is very closing and embedding to
00:03:11.440 creativity in my mind right so this is how I see Ruby um so so talking a bit
00:03:16.840 about Ruby Ruby it's it's it's seems old right 31 years old thank you for that I I did not remember the exact age but bug
00:03:23.720 told US 31 years so it seems like it's it's a kind of mature language and I I wanted to explore a bit some kind
00:03:30.000 evolution of how we do stuff in some languages now I I don't plan to do a
00:03:35.200 historical lesson but just let's take a simple simple problem like how to calculate the sum of of elements of an
00:03:43.519 integral array right so maybe in assembly we can do it like this I did not wrot this code I I search on Google
00:03:50.480 then I I asked CH GPT to explain it it seems like okay seems like to work right
00:03:55.560 it has a jump it has some some uh I remember some registers or this is assembly to me right so this is this was
00:04:02.280 doing a loop and maybe it was better than doing it manually or with Punch Cards Right but if you look at it one
00:04:09.000 one thing about this code that I will say is that it's very detailed and very explicit right like very detailed and
00:04:15.920 very explicit and maybe we got a bit better right so so we we were writing C
00:04:21.400 which which seems to be more English like right still very detailed and and and and and and very explicit and just
00:04:28.240 focus on the essential here like you wanted to Loop through something so you had to decide I I want an iterator let's
00:04:33.360 call it Y and it start with zero or one okay in see it start with zero and the size and you have a condition for
00:04:39.080 stopping the loop and this kind of decision that you had to take right and then and then have the the main thing
00:04:44.639 sum right an accumulator you're putting the sum there and doing doing stuff right and and then someone in this case
00:04:51.039 mats uh was thinking oh what if I can compo I can compress that into a word
00:04:56.400 called each and this is actually screenshot of the each definition in the Ruby language right and so we can write
00:05:02.320 something like this now in Ruby right so so if you look at this one what is happening is that I don't I'm not
00:05:07.919 concerned with what's the name of the iterator where does it start where does it stop nothing about this I'm focusing
00:05:13.360 on the essential I want a sum and this is what I write there right so it's a compression thing even more so Ruby
00:05:19.440 comes with beautiful but also aliases things right so I can do a some just writing some so that's a beautiful
00:05:25.759 compression I don't even have to think about the name of the accumulator inside right and and more so if you think about
00:05:32.840 map what this brings us right is like by using these Concepts that are
00:05:37.880 compressing some algorithms into simple Concepts I can tackle more complex algorithms now this is not exactly lead
00:05:44.400 code complex but just doing the sum of some subarrays right I can write it this way or the other way right and and and
00:05:52.000 for me Ruby if you think about it is doing this kind of compression let's
00:05:57.080 call it but but maybe okay in in in programming we call it abstraction right we we abstract some things and and this
00:06:03.440 is good that we abstract some things because this this makes us write code that we call brev concise code
00:06:12.280 okay brevity I know right concise code so and and that allows us to go from
00:06:19.039 from solving simple problems how to Loop through an array to solve more complex business logic imagine I don't know
00:06:24.880 you're doing some authentication in rails and you write it in assembly the the kind of business l so you're
00:06:30.160 concerned with very very a lot of details that does not matter for that business logic right so so and this in
00:06:36.240 the end for me it's it's a road to Simplicity although Ruby has a lot of standard and core Library I can pick and
00:06:42.800 choose them and I can I can write it simple right so so but but because Ruby is a mature language the the mature
00:06:49.680 languages have a dilemma um and and the Dilemma is that in the Metro language needs to change to remain relevant right
00:06:57.919 it needs to address new problems or and it needs to increase usability and and that that we put it in balance that
00:07:05.160 adding new features or making big changes can disrupt existing code bases so if you remember the the in in
00:07:11.759 startups there is this innovator dilemma let's make this programming languages dilemma right about users so longtime
00:07:18.120 users will want stability and back or compatibility are new users uh and or
00:07:23.840 those facing the pain at the end of what's now at the present right they they they are in favor of more
00:07:29.800 disruptive changes right so so for me thinking about keeping Ruby relevant
00:07:34.919 right it's it's about thinking where I am with this and and here is what is happening I think maybe when if you are
00:07:42.080 learning Ruby today right you look at the features that it has and all of them are maybe exciting for you it does not
00:07:48.360 matter if they were added 20 years ago or yes last year okay everything is new and exciting but if you are using Ruby
00:07:54.840 for a while for a period that's a bit of U the problem of the expert that your mind is so shaped into and and so fit to
00:08:02.960 using what's there and what was there when you started learning right and and
00:08:08.360 when I say mind is true that the language influence is the way you see your word you see the Solutions in those
00:08:14.919 terms in those in those words in those contents right that that you start to become maybe I start to become let's
00:08:21.440 talk about me right I we we tend to start to become conservative and that's not bad right saying okay I want to
00:08:27.199 protect here but there is a risk to this right the risk is when this is going to
00:08:32.399 some defaults like being against what's new right and being against what's new brings us to I'm not even trying it okay
00:08:39.959 I'm not even trying it or trying to use old solutions for new problems and this
00:08:45.680 creates a problem that we're changing the problem to fit the solution right so not we're not solving the exact problem
00:08:51.920 we're say let's solve something else because this is what I know right and and the problem in conservative also
00:08:57.040 about the community cuz i s i I wrote there about community is that the being conservative in this
00:09:02.519 way you become rigid right you become stiff and and and inelastic right and that's a bit stuck in the past right and
00:09:10.279 and and that means that we are you're getting close you don't want to see something different there right and and
00:09:17.640 what is happening is actually by trying to protect so much what we have right we
00:09:23.240 don't see that we are actually shielding it from the future and destroying it in this process so by wanting Ruby to stay
00:09:29.720 the same as it was 10 years 15 years ago we're actually keeping Ruby in the past
00:09:36.600 right and and so uh not fit for the future so it's it's good to be open to new and what does it mean to me it means
00:09:43.040 to experiment to to choose what it works for you I'm not saying ad do blindly everything right and and to and to be
00:09:50.200 open that okay when you're doing something new you are open to breaking and rebuilding to refactoring stuff okay
00:09:57.519 and that that is for me the pet to being relevant so I'm coming back to the question that I I ask like how can I
00:10:03.320 keep Ruby relevant from the place where I am and my solution of course is adob new features experiment with them see
00:10:09.839 what works what it does not work and bring feedback to Ruby committers race committers about about this right and
00:10:18.959 one atics here adob new features means try to use them in a production like Lang environment right not just okay I'm
00:10:26.000 firing up IRB I something there I don't like it because is is not good right try to fit it in a real environment and
00:10:33.600 bring feedback about that okay so I'm going to to show you some code that I
00:10:39.200 know that some some people don't like it in some ways right they they find it
00:10:44.959 weird so I'm trying to to show to you some examples uh of this code and pushing it a bit so this endless method
00:10:51.000 was added in Ruby in the 3.0 in in 2020 this is not chronological I I just put
00:10:56.639 them in order to arrive to pattern matching okay um in some order so how
00:11:01.800 how does it look it's like you have the def keyword and then and then the name and it might be some parameters and the
00:11:07.120 eal sign and then the body here it's one line but endless method is not about being one line you I will show you some
00:11:14.040 examples before right so you might find this word and I I was reading the the feature uh discussion and people were
00:11:20.800 saying this is confusing for for me is this a variable or not and for me is not confusing there is a death before and
00:11:26.440 then there is a eal there right so so I read this very beautifully like Define
00:11:31.720 exists as equal to that active record expression right um so so but but we had
00:11:37.760 on liners before even before this um definition like this is a a valid Ruby
00:11:43.920 um on line and this is another one and and I let some empty space there for a purpose that I will show you and then
00:11:49.839 another one and you saw some other one liners very nicer from back today but so
00:11:56.320 here's my question does that end bring any valuable for you and when you're reading this code do you feel the need to have an nend to know that the the
00:12:02.600 body is finishing there and my assertion is you don't need it right so if I if I'm trying to blur it out more and more
00:12:09.399 right I think for the first line are you confused that the when the curly bracket is ending is that ending no the second
00:12:16.760 line I assert there is a space there but I don't assert that you expect that the body will continue right maybe the third
00:12:22.399 one now bluring out the end it's a bit confusing so this is why I think it's good that we have the endless method
00:12:28.160 right so the endless method comes and says this is the method definition eagle and this is the body right this is the
00:12:35.000 transformation that I see and and let me give you some kind of examples of what I will transform so I was uh doing some
00:12:41.399 review of some open source code for a series on my blog and I I noticed masteron this thing let me zoom in a bit
00:12:48.079 where they have a nice developer experience that they Define some kind of response with some nice labels right and
00:12:54.120 they look quite the same and I was thinking what if I put endless method here right like I I think that's that's
00:13:00.399 not confusing and if I put it I can organize it like this and I know when you read it you you might feel we oh my
00:13:06.120 God so space is and it's aign by eagle and and I don't know it even has comments in in the code right um but I'm
00:13:14.440 saying that putting it like this for me it brings a a nice benefit that I can read it all in in a very few lines I
00:13:21.760 understand about what it is and they are grouped together by their purpose right this is their purpose here so I can
00:13:28.040 group them if I want again I'm not saying you should do it like this I'm just showing you some possibilities
00:13:33.079 right I'm not judging good or bad okay um so why use the endless method yeah you can write small utility methods with
00:13:39.800 it right like this you can you can write small utilities this is I because with
00:13:46.120 SH Ruby I'm watching various uh social media platforms this is an actual code from something that I'm using to query
00:13:52.000 different things um so you can you can do the following you can write small utility methods and make it hard to make
00:13:58.160 them complex you see already this code is a bit complex right putting it one line means that if I want to add
00:14:04.000 something more some other chain I will need probably to refactor it right it becomes too long to par it so in this in
00:14:10.519 this um instance being one line makes it harder to make it complex and I find
00:14:17.040 that a feature okay um and of course because in in a way if you're not in a
00:14:23.720 high performance environment method free in some way right I'm not saying that
00:14:29.279 memory free or performance free but if you're not in high performance you can create quite some methods until you you
00:14:35.480 reach a performance barrier so you can use it for naming so let's say you have this type of code where you have some
00:14:41.040 user settings and some team preferences and then some default ones right so you're writing somewhere this but I can
00:14:47.800 think I can easily without adding too many lines of code so the same number of lines of code I can label them with
00:14:54.639 endless methods right and then this will bring me to do some nice comp position right so so I can write user
00:15:02.399 settings It's that or the other and so on right just presenting some code and
00:15:09.360 you can Define predicates right you could not need to Define them you can say not confirmed at nil and keep using
00:15:16.000 it in the code but can Define some nice predicates simple ones with them because naming is important right and yeah this
00:15:24.040 these new things I will not go into all of them because I want to show you some code but they have some gas they new in
00:15:30.000 some ways like for example the endless method does not support defining um a Setter right not sure if this is a
00:15:37.120 technical thing or it it is because people are saying too many equals in that definition there right it might get
00:15:43.199 weird a bit right but it does not support now I'm not sure but just saying it has some others maybe right but I can
00:15:50.279 push you a bit further so see endless is not one line method right it's it's one
00:15:56.360 instruction or one statement so you can write code like this if you want this is
00:16:01.440 changing a bit the shape of the code right or you can even write with a case here and I will show you some cases with
00:16:06.839 patter matching given right so still an endless method but I I I made a
00:16:12.560 multi-line method okay again not saying write this or not write this but I'm
00:16:17.880 saying let me show you some interesting code right um another feature data
00:16:25.319 class this was added in Ruby 3.2 um 2022 and I'm very enthusiastic about this
00:16:32.160 although maybe it does not change the shape of the code but I found it very useful so what is data class yeah they
00:16:37.519 say it's a convenient way to define simple classes for Value a like objects so it's it's like a simpler struct if
00:16:43.759 you're using struct that has some properties that you can maybe achieve with struct but this has them by default
00:16:49.920 it's immutable um it has a simple interface and it's comparable by value and by type and it has some things for
00:16:56.560 pattern matching you will see and it has a very simple interface these are the only methods here for this uh data class
00:17:04.400 right and let's let's look a bit about it with an example so let's say I have a link and this is how you define it data
00:17:11.160 Define and we can discuss in the break why why there is data Define and not data do new uh but this is how you write
00:17:18.400 it and you can instantiate it like this with keyword arguments you can instantiate it instantiate like this
00:17:25.439 with positional arguments but you can also instantiate it with with this construct alternative form construct
00:17:31.760 that we also saw from back presentation at some point and also with this
00:17:37.039 construct with hash like arguments right and then coming back to
00:17:43.120 immutability it has no Setters so this is you're when you're instantiating
00:17:48.679 you're providing the value for the the the keyword that you're or instance variables from there but you cannot
00:17:55.080 change them it will throw a Frozen no method or error uh um you cannot even
00:18:00.720 change your own custom instance variable so let's say you don't plan to change your error title but you made it inside
00:18:07.480 to change an instance variable that you're defining and it will show you a frozen error you cannot change that
00:18:13.120 right and that is because it's frozen right when it's frozen now just
00:18:18.320 an aeric this is Frozen the same way that you can froze the data structure but if you put a hash in it and change
00:18:24.360 the value inside of a hash you can do it right but I will not go into this uh
00:18:29.960 maybe this is weird things number two okay but it's frozen so why use it so so
00:18:35.640 it's the first thing is like you want to keep data together unchangeable data right so let's see an example real one
00:18:42.960 right you you're consuming an API and you want to to move in your code to send
00:18:48.440 what you read from their further one to be processed so you can define a response like that and then instantiated
00:18:54.200 and passing along along you can maybe work in a in a booking
00:18:59.320 system where you have events and you are already setting up some day start time end time and you want to pass it along
00:19:04.919 right um and you might wonder two things I explain why not struct you can do the same with struct but you have to provide
00:19:11.000 some arguments and so on why not hash right you might ask oh wait a second why do I need that I have hashes right and I
00:19:17.880 will not go into all details why not but just sharing you about the shape of the code so here are some examples about
00:19:24.240 hash one hash is like an anonymous structure right so can you
00:19:29.559 grab all the cases in your code where you have the structure hard right um but
00:19:35.520 you you can grab response if you want with LSPs you can go to Source right very quickly or go to Def to where
00:19:42.320 useful instances right but more so you can add computed attributes on the data
00:19:48.159 defined right simple attributes okay I I made it endless but you don't have to make it endless right um so simple
00:19:56.480 simple uh this this uh uh you can make like this which you cannot do in hash you can even use them in nested right so
00:20:03.520 here I'm using R limit um and I'm putting it there I'm instantiating it and I don't need to change this because
00:20:09.679 I just read them right and and yeah another example of computed attribute
00:20:15.840 and it removes boiler PR code and usually I could have achieved the Same by doing this this simple plain or Ruby
00:20:23.559 object right but this is just replac with this it even supports inheritance if you like you can make it like this
00:20:29.880 right um and here is a a real example from the Theoden project that I found
00:20:36.200 and and they Define here uh data object and then they have a list of reasons that they are objects right
00:20:43.600 because they have some attributes and then they they hook it up to raum here
00:20:48.960 right and then they can use it in the view like this and of course you can do
00:20:54.159 again you can do the same with hash but this brings you a nice object or or
00:20:59.200 oriented organization right you you have that object you know how it is if you have to change it you have a way to know
00:21:06.039 where to change it um another one and this one is I think maybe the most controversial one because we are even
00:21:13.159 debating sometimes now the change between hash with the key hash rocket keys and the simplified one I don't
00:21:20.520 remember the name right so we added something more but let me go to a bit to it right this was added in Ruby 3.1 2021
00:21:27.880 so how it let's say you we have this type this small code that someone wrote
00:21:32.960 it named differently for some reason so let me just rename it a bit to have good names right to use the same concept url
00:21:39.400 url not website and so on right a bit of renaming so now it looks like this right
00:21:44.880 so so then my here is my assertion here do I maybe need these things here right
00:21:50.919 this seems like repetition in a way and that is a pitch for this hash roet right so hash because if I'm writing it like
00:21:58.320 this this is Hash value a literal Omission it works what it does it's you
00:22:04.840 put a key and it searches for a local variable or in your scope with the same name and use it as a value right and I
00:22:12.000 know my seem we but as you're starting to reading it you you become account custom with now let's let's take a more
00:22:18.760 complex uh example let's say we have a link and a null object of that link with
00:22:24.080 data Define and I don't know some kind of validator for the URL and and let's say it's somewhere in the code this is
00:22:31.440 part of a class let's say but I don't did not put everything here you're doing something like save link with some
00:22:36.840 intermediate steps but I made it simpler a bit here right so so again maybe this
00:22:42.600 is written in different parts of the code in the same file but using different names always naming it's it's
00:22:48.919 it should be for me standard or consistent right so let's let's let's look at it it's website url url url an
00:22:55.919 even address right and for me thinking about hash literal value means I want to bring them together so I just rename
00:23:02.559 them to mean the same way to to to be the same naming so now what I can do is
00:23:08.080 I can get rid of these extra things here right so now this code right the the
00:23:14.159 value on top flows through it and I can only focus on the top part of it and I
00:23:19.919 know if if I change that everything follows it's just an example okay about this so why use it I think from my pitch
00:23:26.880 this is simple I I don't have more examples if you want to use it is mostly simplifying names and reducing
00:23:32.080 repetition right so it's it's about being concise and and being consistent in the naming it it it um forces you to
00:23:39.760 to use the same name for the value as the key right that's that's the main thing it or as does not work right um
00:23:47.799 and and now I'm just introducing a smaller one argument forwarding this is in Ruby since 2.7 2019 and what's it
00:23:54.760 argument for forwarding I have the same Source but if I do a change here here and I put this three dots I want to say
00:24:02.240 that everything that comes after URL will be passed on to the link thing everything so what does mean that means
00:24:09.400 that means if I want to do a change let's say someone says Ah we're reading a list of blogs so we want to have the
00:24:14.600 outdor there right I only have to change the call to that method but the body
00:24:19.640 Remains the Same and it's fine for the body to remain the same because the body has the same purpose instantiating and
00:24:25.559 validating the URL is not concerned with the other things there right uh yeah and
00:24:31.640 it brings me to actually the thing that I think has the the possibility to change the most the shape of the code or
00:24:39.000 the code design that we are using and here I will just show you some examples but this this uh concept is in more
00:24:45.399 complex than what I will show you more complex in the sense that you can do more with it right but let's let's take
00:24:50.720 it again with an example you have um you have you have to parse or ingest some
00:24:57.559 kind of Json that looks like this and you only have this type for now that's the request you have a push notification you have to do something with you so of
00:25:04.120 course I I'm making a structure a data Define and then what I can do so because
00:25:09.240 the type is push okay I don't know what other types are but I want to make sure that I'm only processing this type that
00:25:15.679 I'm sure of right so I can write this code which which is pattern matching and I can say if the Json that you're
00:25:22.360 providing me has the following structure type push and then some other attributes that I'm concerned with the
00:25:28.840 I can instantiate what new and then you might say hey but where the title body
00:25:34.240 and push time are coming in that thing there I did not define those there so
00:25:39.360 let's let's let's try to debug it a bit right so let's try to print local
00:25:44.559 variables here what do you think will the the line nine will will print what local variable names we have
00:25:53.640 there notification right that's the only local variable this seven it's a
00:25:59.399 constant okay it's not a local variable okay and what do you think the line 13
00:26:04.720 will print exactly exactly yeah you got it
00:26:11.120 already I can stop with with this yes so it creates local variables now just a
00:26:16.240 bit of um so there is that and there is this right so a small warning here right
00:26:23.600 it creates local variables if they exist it will change their values you can
00:26:29.080 force it not to change it by using this spinning operator I will not show too
00:26:34.159 too many examples of this but there is a way to not change it if you want to right okay so let's let's make this a
00:26:39.799 bit more oh I'm in op in Ruby I want to create some some um methods and I want
00:26:44.919 to create a build like method preparing for maybe different types right so I'm I'm putting it like this and and and
00:26:52.080 okay this is simple so this is a way to write it with in and if you do if
00:26:57.840 something in something else that in is responding with true or false right it matches or not but you can use another
00:27:05.520 operator which is called right word right word operator which is an exclusive operator that means if it does
00:27:11.760 does not matches it will throw an exception right so now I if I want to do
00:27:17.440 this I can catch it and log it and return something else if I want to right
00:27:22.520 so now now I don't know someone says hey we want to support more types right more
00:27:28.279 types so email and and SMS and they have other other attributes right so I can
00:27:35.080 use pattern matching I Define two new defines let's say and I now I'm now
00:27:40.799 using the case in pattern matching the third way to use pattern matching if you really need this right so what is
00:27:45.919 happening here this is matching on the structure right so I say if it's type is p push then I have I want to take the
00:27:52.320 title the body and and the push time if the type is email I want to take the subject the sender and some other stuff
00:27:59.000 and if the type is SMS I want to do message phone number and so on right um
00:28:04.880 uh this is kin so why use it and or some examples one is check the type of a
00:28:10.960 variable but I want to say a wording here Ruby it's a dynamically typed
00:28:16.519 programming language I take this chance to say Ruby has types when people are saying we want types we have types in
00:28:23.080 Ruby that that's not the discussion right it's dynamically typed and and the second thing thing here is that if
00:28:29.399 you're doing explicit typeing be aware that you're fighting Ruby flexibility because Ruby's op dynamically typed we
00:28:37.120 care more about the method or the does this object respond to this name and
00:28:42.559 less about is this the this type of object or it has this name but you can do it maybe you finded yourself right in
00:28:48.039 this type of code right doing something if it's a string or array or symbol right and you can use uh pattern
00:28:55.440 matching if you look a bit at this looks almost like type checking there right it's very string that's beautiful
00:29:02.559 beautiful line I will say right of course remember this is a an exclusive so it does not matches it throws error
00:29:09.559 you can use it like this if you don't want to throw an error it in right unless in string this is simple you can right and
00:29:17.440 you can do more you can say Okay I want to check just some parts of the hash structure so let's say I'm reading some
00:29:23.360 Social Media stuff and I'm only caring about hashtags here so so I care that it
00:29:28.760 has inside the keyword name hashtag so this will work um but these two will not match
00:29:36.559 right because they don't have the Q or hashtag but it it will not throw an error it will just silently go over right and you can write something like
00:29:43.840 this that can look like this if you want right case value it's symbol then do
00:29:49.960 something if it's array to something else right you can also put it
00:29:56.519 andless here right I remove the end if you really want to I'm not saying but
00:30:01.760 okay but now what's more interesting you can use it for error handling and validation so that was very simple to
00:30:07.480 say this is array or string or something you can do it with some other methods right you don't need pattern matching
00:30:12.840 for that but let's say you want to say I want to do something if there is a hash with name that is string and age that is
00:30:20.399 an integer and if it's not then it's invalid and you can do this right here um you want to make that or
00:30:28.600 you can even match more complex structures right you can destruct destruct more complex structures with
00:30:34.640 this right um and this is this is pretty quote that that I wrote not exactly this
00:30:41.080 one all of these are some kind of anonymized in a way so not exactly the code but similar right so we are
00:30:46.919 ingesting some kind of response and sometimes we want the response comes with object customer or object invoice
00:30:53.640 strip like things and I want to instantiate some object out of them and you can do this with it and here is an a
00:31:01.240 more complex in a bit example where I I say the following you can match against your own custom object you saw they're
00:31:07.840 matching against hash right hash shape but you can match against your own custom object so let's say I'm in rails
00:31:14.080 I have some uh active record stuff structure here maybe not the best for authorization but let's say if I add
00:31:20.720 this method that is destru keys and it has a parameter keys but because I want it simply I'm not doing anything with
00:31:26.679 that but you can do right and I say okay when you represent user as a hash represent it like this and when you
00:31:33.480 represent admin add something extra because it's an admin right then I can
00:31:38.840 do something like this I can display a message to the person that is logged in by matching against that
00:31:45.760 structure so if it's a if it's an admin uh sorry if it's an admin with a role
00:31:51.200 owner do something if it has permission do something else if it's an user with a role member do something if it's a team
00:31:56.760 Le do something else right right and again a simple thing patter matching supports in something if so you can do
00:32:04.120 more but I I want I needed to contain a bit myself so I will finish very very quickly so what now what now is that
00:32:10.960 choose something that you want to adopt let's say right write a read your codebase and find some places that you
00:32:16.919 can use it don't invent them write right write a proposal about it and debate
00:32:22.480 with care and and cooperation right so here is me I work for cookpad at some point and I I the pr to change rubocop
00:32:31.159 settings rocop supports writing this code probably in all the ways that you want because it has a lot of settings
00:32:37.320 right so is not if you think about rubocop is not something that it restricts you you have to configure it
00:32:42.760 and here I'm changing the configuration and this is not the pitch this is the actual pitch for this I explain examples
00:32:50.039 why to use it how what does it bring and so on and so forth right so write good pitches and that's it for me um if you
00:32:57.960 like you can subscribe to my newsletter newsletter ruby.com um if you like what you saw I'm writing
00:33:05.399 some kind of email courses here they are not ready but they're close to ready you can go to learn. short.com and put your
00:33:12.039 email there I will send you when they are ready uh more about this I just pick and choose some details but I have more
00:33:18.639 about modern Ruby um and as as uh as I mentioned in the beginning I'm Co
00:33:25.200 organizing friendly RB which which will be in September go to the website if you
00:33:30.320 want to to participate um friend RB we have some beautiful already speakers
00:33:36.080 there but uh there will be more because the call for for presentation is not yet finished um and that's it for me thank
00:33:43.039 you very
00:33:49.519 much I think I took a of mind blown thank you so so
00:34:06.480 Suspects that's why I sit here to intimidate the speakers so uh my personal dislike for
00:34:14.079 many of the features just because every time a new feature is introduced we have to change the parser and write a lot of
00:34:19.599 new codes in rubocop uh for all those options that you mentioned I I'm wondering what's your take on one thing
00:34:26.000 um most of those features at least for from my perspective are superficial they're not gamechanging okay we the the
00:34:32.679 code became uh more brief more concise debatably more readable or less readable
00:34:38.760 but does it really um help us secure a future for Ruby for the next 30 Years
00:34:46.040 yeah very good question um so so I I cannot say which one of them is the best
00:34:51.720 one right uh and it might be that the current ones maybe that's the the part of the question right maybe the current
00:34:57.520 one are not fit right but what what I'm saying is that in order to find out
00:35:03.480 something that that is useful we have to go through experimentation right we we
00:35:09.200 have to experiment with them and say okay I try to use endless method let's say right in my code and in somehow
00:35:15.720 after using it for a while I discover it's not working right so we have to provide that feedback right or or say ah
00:35:21.800 I need to support Setters even if it looks weird because I I found it useful
00:35:27.040 in this context right um so some of them are more compression than than logic changing so it's like I want this to be
00:35:34.720 to express it in a smaller I don't know way some of them I think for example I'm I'm I'm more
00:35:41.640 enthusiastic about pattern matching in some cases and about data the the data one and the pattern matching I see data
00:35:47.480 being a bit on the foundation to adopt maybe if we want more functional things
00:35:52.960 because it's immutable and maybe go to making immutable the full structure of a
00:35:58.040 hash or so on I'm not I'm not saying this is happening but I'm saying maybe it's it's on the road toward that if we
00:36:03.480 like the immutability part right so I cannot say these are exactly changing but I can see that if we're
00:36:09.720 experimenting them and give enough feedback because I I read for all of them the discussion on the Ruby issue
00:36:16.319 tracker right and always two things here one I really like how mat is approaching
00:36:21.839 this you saw this launching in the last years but if you look some of them are debated for 10 years or seven years that
00:36:28.680 this was not accepted easily right and and mat is always asking what's the
00:36:33.720 usefulness of this and you have to pitch in to express your pro or cons or or put
00:36:40.640 some edge cases that might not be useful or might be useful right in order to convince so I think we should use it and
00:36:47.839 and provide feedback and maybe this will bring us to a more um how say a period more like prian period of
00:36:55.359 experimentation right um that's that's my take great answer thank you who's
Explore all talks recorded at rubyday 2024
+1