ruby, ruby on rails, software design, best practices

How to Learn the Syntax of a Programming Language When You Know How to Code

Red lego blocks

The Ruby language's syntax has some basic building blocks

Do you have a hard time learning the syntax of a new programming language, even though you know how to code? Here's a quick knowledge transfer exercise to help you learn Ruby.

Are you having a hard time picking up the syntax of a new programming language, even though you know how to code?

You’re new to Ruby, and people say it’s an easy language, but you find the syntax difficult to understand.

You know a bit of Javascript, Python, Java, or any other language. But Ruby seems like alien technology to you.

By the end of this post, you’ll have a tool to help you transfer your knowledge of [INSERT PROGRAMMING LANGUAGE HERE] to Ruby.

Ruby is all about messages and objects

Ruby is all about objects sending messages to other objects. It’s a slightly different way of thinking.

Ruby is similar to languages like Smalltalk and Lisp. They’re very flexible languages based on a small number of basic building blocks.

That flexibility allows you to make your code very readable and descriptive. Almost like writing English text.

Learn the Basic Building Blocks

To learn the syntax and the style of a new language, you should understand the basic building blocks of the new language you want to learn and compare them to another one you understand well.

Then you can try to translate a small piece of code from one language that you understand well to another you’re trying to learn.

That’s how you spot their differences and similarities and understand how to apply these ideas in different scenarios.

This is something similar to Transfer Learning, or Knowledge Transfer.

You can use the knowledge you already have about how one language works, and use that to help you pick up the syntax of another language.

Got it? Let’s practice!

How to transfer coding knowledge from one language to another

Here’s an exercise you can use to transfer knowledge from one language to another.

1. Start with a simple problem

Always start with a simple problem you can easily solve with just a few lines of code in the original language.

Let’s assume you know Golang.

How would you write a piece of code that prints numbers from 1 up to 10 in Go and Ruby?

Maybe something like this:


// Go
for i := 1; i <= 10; i++ {

This is one way you could do it in Ruby:

# Ruby
(1..10).each do |n| 
  puts n

2. Identify similarities and differences between the basic building blocks

What are the big differences you can spot at first glance? Do you see any differences in style?

In the Ruby example, here are some of the basic building blocks we can spot:

  • a range 1..10
  • the usage of an each iterator instead of a for-loop
  • a block do ... end with a parameter n
  • a puts n method called with one argument and no parenthesis

3. Ask yourself some questions

Here are a couple of suggested questions you can start with:

  • Ruby has for loops, but why are we iterating over a range here?
  • Is each a method call on range?
  • Why are we using do |n| ... end instead of { |n| ... }?
  • What if you change the code to puts(n)?

You could even ask a slightly harder question:

  • Would you be able to send a message to a variable in Go?

This is how you can send a message to an object in Ruby:

# sending a message instead of calling a method
 (1..10).send(:each) { |n| puts n }

Do you see what I mean?


To recap:

  1. pick a simple problem you can easily solve with just a few lines of code in the original language
  2. solve the problem by writing code in a language you know well
  3. solve the problem by using the new language you want to learn
  4. try to spot the basic building blocks, styles, and patterns in both pieces of code
  5. compare both implementations to see any differences and similarities
  6. ask yourself some questions and try to answer them

You can learn a lot about Ruby (or any other language) just by trying to answer one of these questions above.

And then trying to write a similar piece of code by yourself in the new language so you can practice what you just learned.

You will learn not only the style and syntax of Ruby but also the mindset behind it.

This exercise will help you pick up any other programming language. Change the questions a bit and practice.

Now go practice!