A little background: I am a self-taught UX Dev. I know somethings but I would rank myself as an advanced noob. I know some PHP and some JavaScript. I know somethings but I am woefully lacking in others. I recently started the DPI crash course in hope to make it into their Software Developer Apprenticeship (more info here). Currently I have started learning Ruby in their prerequisite Coding Crash Course.
I won't recap the whole of my notes, just an analogue of what an expressions is and what it represents. This really helped drive home the concept. Stay with me here, I promise it makes sense, at least in my head.
I like to think of expressions like a character actions in dnd. Expressions are like actions that you want the character to take. You must have a thing and a thing doing something.
"DM I want to drink this potion." Potion is an object. (a thing). However, just a thing by itself is not very useful. Drink is the doing part of the action. Our action is that we tell our DM is similar to how we write the most basic of expressions in Ruby. An expression is made up of two main parts, the object and the method. The object is a representation of information. The method is what we are doing with that information or thing. In Ruby this action could be represented with the expression potion.drink
.
Now we can take this example in a couple different ways. We could give the method an argument of how much we want the potion to restore our health. potion.drink(10)
. For the sake of this example we can take this even farther by nesting another expression inside the expression and assigning it to a variable (inception, almost). drink_potion = potion.drink(health.add(10))
. For the sake of this example lets pretend that the potion class functions like adding a number to a string. Just pretend there is a potion drinking framework out there with me.
If we wanted to not pretend that there is a fictional dnd ruby framework, this example also kinda works.
current_health = 2
max_health = 12
potion = 10
drink_potion = currect_health.add(potion)
current_health = drink_potion
pp current_health # this will return 10
Anyways,
tldr: ruby expressions are like dnd actions trust me bro.
Top comments (3)
This is a fun way to think about Ruby expressions! I never considered the analogy to D&D actions, but it really helps to visualize the concept. Thanks for sharing!
Thanks! I am a very visual learner and the grammar analogue of the expression being a sentence wasn’t capturing my imagination (also a bit boring 😅). This really helped me think of the possible use cases.
This applies to OOP in general, not only Ruby. Benefit of ruby is that it's very clean and descriptive.
Using game concepts is great way to explain and learn OOP in my experience - my first programming experiments were writing some d'n'd-like game.
Let's go bit further and implement you examples into mostly working prototype:
(I didn't really implement
Current
, but it's just some place to containt current player).So, interesting thing here is, action can be
potion.drink
ORplayer.drink(potion)
. It depends on us which is more clear to us developers (and on the scale of game and such).