DEV Community

Rebecca McKenzie
Rebecca McKenzie

Posted on

Syntax, syntax, syntax

I wan't sure what to expect when I first started learning Ruby, but the experience was more enjoyable than what I expected. A learning curve with most programming languages is syntax. Going in I had Python and JavaScript (with other front end languages), under my belt. Python was the most similar in general and made Ruby feel familiar. However, with the familiarity I found myself having to overcome certain syntactical issues, and feeling liberated as programmer because of certain advantages Ruby has over Python.

Looping(Iterating)

One of the most powerful things you can do in both languages is looping, or in this examples case iterating over a range of numbers.

for j in range(1, 8):
  print(j)
Enter fullscreen mode Exit fullscreen mode
(1..7).each do |j|
  puts i
end
Enter fullscreen mode Exit fullscreen mode

These both print a list of numbers, 1-7. I appreciate that Ruby has inclusive and exclusive ranges. However, it is interesting in Ruby looping, if statements, and functions require an end statement. Looping in Python doesn't and in hindsight when there's hundreds of lines of code, more end statements help to make code more clear. The colon in Python loops has also caused issues in program execution because it's one of those things that is so easy to miss in a sea of code.

Dictionaries and Hashes

Python dictionaries and Ruby hashes are another case of differentiating syntax. Python only allows for a colon to be used for assigning values to keys, while Ruby allows for a => to be used as well as the colon.

peeps = {'Janet': 1234, 'Samuel': 5678, 'Georgina': 6060}
Enter fullscreen mode Exit fullscreen mode
peeps = {:name => 'Georgina', :number => 6060}
Enter fullscreen mode Exit fullscreen mode

Ruby also has different data types like Symbols, so using the => ends up being a default practice because it makes the hash easier to read.

No Nests, No Problem

Lastly, I found myself wanting to do certain, "small" things in Ruby that weren't possible. When learning about methods I found myself wanting to nest them in the way you would Python functions. In certain projects this was annoying, but ultimately helped me learn what Ruby isn't as a language. Python is easier to learn imo than Ruby, but when you get the hang of things Ruby is more enjoyable to use as a developer which is a huge point of its existence. To be fair I've primarily used Python in a design, scripting, artistic lens as opposed to web development with Ruby. I want to go further into database management with both languages. I'm looking forward to my findings.

Top comments (0)