Ruby I | Syntax — ‘What did you say?’

Milan Parmar
3 min readDec 20, 2020

Ruby

Welcome to the first instalment in my Ruby series…Syntax!

Ruby was the first programming language I started to work with, whilst at Flatiron School. Our first ever session on the program was an introduction with cohort leads where we could ask any questions and get an idea of the curriculum. So, what did Milan ask?

‘Why Ruby?’ 👀

Well, I knew that there are other languages more popular and in demand so I wanted to know…why Ruby?

The answer I received was enough to get me excited and dive deep into software engineering. One of the leads emphasised how Ruby is dynamic, in the sense that you can achieve a result by different paths; you can achieve the same result by writing Ruby code in different ways. He also mentioned that other languages (not all) have only one way to achieve a desired result. So why did this excite me? Well, it gave me scope to tackle problems with different approaches, which allows me to learn and adapt, and taught me how to code more efficiently.

Since this was my first language, one word that became prominent in my vocabulary was syntax! Snorkels on? 🤿 Let’s dive in!

Strings

Strings are characters between two quotation marks and are used to represent text and data. You can type out strings with either double or single quotation marks.

"Hello world, my name is Milan" || 'Hello world, my name is Milan'

String Interpolation

Interpolation allows us to merge variables into strings and you will find yourself doing this quite often when working with Ruby. In order to use string interpolation, you’ll need to use #{}.

brand = 'Samsung'
model = 'Galaxy Z Fold 2'
rating = 5
'I own a #{brand} #{model} and give it a #{rating} out of 5!'

Arrays

An array is an object used to represent a list of objects and can contain any kind of object such as a string, integer, float or even another array. You can create an array by using the literal constructor [] (square parentheses).

numbers = [ 1, 'two', [ 3, 4, 5 ], 6.0, "Seven" ]

Hashes

A hash is a data structure consisting of key-value pairs. You can access hash elements through their keys, which are unique. You can create hashes by using ‘curly’ braces {}.

Looney = {
Bugs: 'bunny',
Daffy: 'duck',
Porky: 'pig,
Elmer: 'human'
}

We can access a hash value by doing the following:

Looney[:Bugs] 
#'bunny'

Method & Class Definition

Ruby methods are created to contain a specific function/goal and this goal could be to obtain information or modify objects. You can create methods by using the def keyword followed by the method name and closing with the end keyword.

def method 
#code specific to method
end

Ruby classes contain methods and have similar syntax but without the def keyword.

class Example
#methods
end

If/Else/Elsif Statements

If/Else/Elsif statements allow us to verify certain conditions and so receive a particular result. They also close with the end keyword.

temperature = 30 #celsiusif temperature < 15
puts 'Wear a jacket, scarf and maybe a hat.'
elsif temperature.between?(16, 25)
puts 'A thin sweatshirt would do.'
elsif temperature.between?(26, 35)
puts 'Suncream, a cap and enjoy the heat.'
else
puts 'Leave Earth NOW!'
end

Case Statements

Similar to If/Else/Elsif statements but with different syntax. case is the condition we are looking for and when we meet the condition, we create an output.

case 80
when 1..20
puts 'Battery level too low!'
when 21..50
puts 'Okay battery level.'
when 51..80
puts 'Good battery level.'
when 81..100
puts 'Excellent battery level.'
end

In the example here, .. is referring to the range of numbers so 1..20 is a range between 1 and 20.

Understanding the basic syntax for Ruby is quite easy, as it is straightforward and readable but syntax is extremely important, especially when working with a lot of languages. For example, you do not want to be writing out JavaScript code and accidentally use Ruby Syntax — major error alert! 🚨

I hope this gives you a helping hand and some guidance when coding with Ruby! Do check out ruby-docs and rubyguide for more on syntax.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Milan Parmar
Milan Parmar

Written by Milan Parmar

Software Engineer 👨🏽‍💻 | Full Stack Web Development 💻 | Smartphone Tech Enthusiast📱

No responses yet

Write a response