DEV Community

Kailana Kahawaii
Kailana Kahawaii

Posted on • Updated on

In case of case and other mnemonics for remembering syntax

When I first started learning a foreign language (Japanese in my case), I kept track of the words through rote memorization. But in college, I learned a different way!

My professor had a good way of drilling vocabulary and grammar structures into our heads: by creating phrases so senstaional it’d be hard to forget. Even now, I can remember some of those bizzare analogies. Masaka, massacre — no way, there’s been a massacre.

Code really is just another language after all, and it helps to remember syntax and structures with a fun mnemonics device.

Markdown

I struggled for the longest time to remember the order for linking a… link. Finally, someone suggested SR, square round, Sally Ride, and it just stuck.


[square](round)

Enter fullscreen mode Exit fullscreen mode

Case Statements

Case statements are a bit different in most languages but the structure remains the same: a case declaration, a when condition, a result for each condition, and a result for all other conditions.

Although I knew the structure, I would often forget to pass in the argument through the declaration (as some languages expected). Now, when I get an error, I just remember in case of case.




case some_value

when object = 1

    puts “object is one”

when object = 2

    puts “object is two”

else

    puts “object is not one or two”
end 

Enter fullscreen mode Exit fullscreen mode

Ruby

Mnemonics are especially helpful in Ruby, which uses (mostly) descriptive language to define its methods.

Take and Drop

Given an argument, drop removes an amount of elements equal to the given number. This is different from take, which takes the amount of elements and returns them.


[1, 2, 3, 4, 5,].take(1)



=> [1]



[1, 2, 3, 4, 5,].drop(1)

=>

[2, 3, 4, 5]





Enter fullscreen mode Exit fullscreen mode

tr

The tr method replaces characters of a string with other characters of a string. I like to think of it as directly translating the characters, as opposed to substituting a sequence (in which case you would use gsub).


I swear.tr(swear, *****)



=> I *****



You got grit.tr(gt, 12)



=> You 1o2 1ri2





Enter fullscreen mode Exit fullscreen mode

Strings

You likely know about strings in Ruby. But, if you wanted to use Heredocs you’d need to construct it using the <<- operator. I’ve always thought <<- looks like a fallen pine tree, and what do you make documents (paper) from? Trees!




document = <<-HERE

This is my text.

HERE



Enter fullscreen mode Exit fullscreen mode

Ranges

For array ranges, two dots indicate the range is inclusive while three means the range is exclusive.


>>arr[1..3] # .. inclusive

=> [5,1,2]

>>arr[1...3] # …threexclusive

=> [5,1]

Enter fullscreen mode Exit fullscreen mode

Conclusion

There are a bunch of tricks for remembering syntax and procedures. What are some of yours?

Top comments (0)