Comparing Ruby Syntax to English Syntax patterns

Nicole Candiotti
4 min readMar 7, 2022

--

A blogpost for the grammar nerds in your life. Basic knowledge of Ruby and Active record would be beneficial in your reading (…coming from someone with exactly two weeks of experience with those concepts. I digress … )

As someone transitioning from speech-language pathology to software engineering, I often think to myself, you just can’t take the speech-pathologist out of the girl. As I’ve worked my way through the JavaScript, React, and Ruby phases at Flatiron School in the software engineering program, I often find myself relating the language patterns I see in computer programming languages to those of languages I already know (English and French). As someone who is deeply interested in linguistics, particularly syntax and morphology, I’ve observed that we use the term “syntax” daily, though I often find it is not used exactly the way I think of it in spoken languages. If you think about the concepts of syntax and morphology as I’ve known them my whole life, you might have a mental image of something like these trees below :

Morphology Tree (rehospitalize) and syntax tree (Happy linguists make a diagram)

If you are not familiar with this idea, in linguistics these are considered “Concrete Syntax Trees,” which parse a string. The string, or sentence, is the root of the tree, which branches out into nodes (in the case above, to a noun phrase and verb phrase node, or branch). These branches continue to bifurcate until each word in the sentence is identified as its own unit. Computer programming also uses syntax trees; however, they are considered “Abstract Syntax Trees.” As someone who was not as well acquainted with the concept of an abstract syntax tree, this was the first thing to pop up on a quick google search :

Kinda scary, right? It got me thinking :

If we were to translate the Ruby we write in the context of Active Record into English, what would its basic sentence structure be?

If I were to distill it down, I’ve decided :

Verbs == methods

Nouns == variables and classes

Adjectives == attributes

Let’s break it down further — Harry Potter style

I’ve create a Hogwarts database with wizards, spells, and spell books. A wizard has many spells through a spell book , a spell has many wizards through a spell book, and a spell book belongs to both wizard and spell. Our schema might look something like this:

When we create the migrations, each table will become a model (and class). I like to think of these as the nouns. Each table has several attributes, which I think of as its adjectives. The attributes are similar to inherent traits or characteristics of its class. All wizards have a name and skill level, all spells have an invocation phrase… just like all pizzas have cheese and sauce. It’s just who they are at their core!

Once we create our models (Wizard.rb, Spell.rb, and SpellBook.rb), it’s time to write the associations. The associations for Wizard might look something like this:

The associations are incognito instance methods, or what I like to call, Ruby verbs. We can call class methods on classes and instance methods on instances to invoke behaviors. For example, if we wanted to know all of the spells for the first wizard in our data, we could enter into the console :

Wizard.first brings retrieves the first instance of the Wizard class, and Wizard.first.spells retrieves all of the spells for that wizard

I also consider methods like .map, .all, .where, .find etc. to be in the “verb” category. These methods (verbs) take a noun (class or instance) and perform an action on it. I think in some situations, enumerables and loops can also function as adverbs, modifying a code block (e.g., a while loop could be seen as an adverbial phrase modifying whatever method (verb) is running until the condition is met)

The last syntax concept that’s piqued my interest when learning computer programming languages is sentence structure itself. In English, the simple sentence looks something like “ I write code,” which is a Subject-Verb-Object structure. In our Harry Potter example, if we wanted to add a behavior that allowed a wizard to learn a new spell, we could write an instance method that takes a spell as a parameter and creates a new spell for that wizard, something like this :

If we look at the method itself, to me the syntax speaks more to a verb-subject-object syntax. Similarly, to print something on the console, we use “puts ____” with the verb (puts) preceding its subject. However, in the instance method shown above, when the function is called, it does abide by a more subject-verb-object structure (e.g., Wizard.first.learn_spell(Spell.last).

If you’ve made it this far, cheers to linguistics! I hope to further explore this concept with other languages, including JavaScript and React and welcome any friendly debate on syntax (Concrete or Abstract) in the comments. Happy coding!

--

--

Nicole Candiotti

Software Engineer | Speech-Language Pathologist | a11y | Linguist