DEV Community

Cover image for Ruby: An Intro for the Curious-Minded
Gabrielle J.
Gabrielle J.

Posted on • Updated on

Ruby: An Intro for the Curious-Minded

If you're new to programming, Ruby is a great language to start learning. Ruby is a general-purpose, object-oriented programming language known for its readability and flexibility. It's primarily used for web application development, and is strongly associated with the popular web application framework Ruby on Rails.

Designed by Yukihiro Matsumoto in the early 1990s, Ruby was created primarily to accommodate the needs of
"humans, not machines". This article aims to break down why Ruby is something you should consider adding to your roster of useful things to learn as a budding programmer.

As mentioned earlier, Ruby has many advantages.

The same way that dictionaries are updated to include new words, Ruby also allows you to change how the language itself is used. You can use Ruby on many different operating systems. It is useful in many different types of professions - software development (both server- & client-side), development operations (aka DevOps), quality control & database management, amongst many others. Ruby developers are one of the highest-paid developers in the market, even though Ruby is not as well known as JavaScript, Python, or TypeScript.

Basic Syntax:

Here's an example of assigning a value to a variable and printing it. Notice that the variables are not explicitly declared, and the simplicity of its print statement:

my_Numeric = 115
myBoolean = true   
my_string = "Hello World"

puts my_string   // prints Hello World
puts 'Hi ' * 3   // prints Hi Hi Hi

Enter fullscreen mode Exit fullscreen mode

Methods, Conditionals, and Arrays

Much like in JavaScript, methods are called after the variable and a dot.
Notice that the space between the array items doesn't affect the output, and the use of zero-indexing and bracket notation:

list = [3,2, 100, -54]
puts list.sort   //   prints -54, 2, 3, 100

daytime = 'During daylight savings time, the sun is still out at 8 AM.'
daytime.gsub('AM', 'PM') //  .gsub finds and replaces all occurrences of the 1st parameter with the second parameter

Enter fullscreen mode Exit fullscreen mode

The first variable in the example below is an array with three elements. Using the index [1], you can access the second element.

The second variable points to a hash, which is a collection of key-value pairs. You can access the value next to the AM key by typing person[:AM].

veggies = ["cucumber", "celery", "lettuce"]
puts veggies[1]  # Output: celery

person = { name: "August", AM: "Good Morning!", PM: "Good Night!" }

puts person[:AM]  # Output: "Good Morning!"
Enter fullscreen mode Exit fullscreen mode

And here is a simple control flow conditional statement and a loop:

The first block of code checks if y is greater than 5 and prints the corresponding message. The second block uses the times method to iterate five times, printing the iteration number.

y = 2

if y > 2
  puts "y is greater than 2"
else
  puts "y is less than or equal to 2"
end

5.times do |i|
  puts "Iteration #{i+1}"
end
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we explored the fundamentals of why Ruby is a relevant and popular addition to your arsenal of knowledge and provided code examples to help you realize how simple and flexible Ruby can be. I hope this has been a beneficial and exciting introduction to Ruby.

Happy Coding!

Photo Credit: Ilya Pavlov @ Unsplash

Top comments (0)