Getting to Know Ruby: Day 1

Published on: August 20, 2020 | Reading Time: 4 min | Last Modified: August 20, 2020

ruby
basics
hello-world
vasiables
operators
input

Ruby is an interpreted, high-level, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. I never really thought I'd learn Ruby at any point. If you look at the market both Ruby and Rails are declining in use, JS is increasing it's market share on a daily basis, if I wanted a scripting language I had python which to be honest has more users and market share than Ruby. But recently I've really been looking at Web Services and that is when I saw that Ruby and Rails is still used widely for a bunch major players in our industry. Now this sort of caught my attention and I started contributing to projects that was build with rails and it required me to hack in Ruby. One thing about programming languages is that once you get the basics down you could sort of port that knowledge to any language and all you need to worry about is syntax. This was the same for me too. I copy pasted a lot and looked at commits from other contributors.

By this point I started to listening to ruby talks and saw a few talks by Matsumoto, he is an interesting person. He seems like a really nice guy, you don't get the usual 'Genius Vibe' common with other Language Creators. He says that he created Ruby for developers, that his primary goal is to make their lives easy and more productive and while remaining very performant. He goes on to say that Ruby focuses on productivity and joy of programming. I find that to be sweet. This felt like something different and I decided to get a taste for Ruby firsthand. So I decided to learn Ruby.

Ruby like python has a REPL, it's called the Interactive Ruby Shell or IRB. If you've installed Ruby you should be able to open your terminal and run irb and the shell.

Printing Hello World

Well first things first. Lets make Hello World appear on a screen. We do it like so:

1
print "Hello World"

Another way to do this would be:

1
puts "Hello World"

Well what's the difference between the two? If you entered these commands in the irb you'd see that put prints the statement and goes to the next line while print does not enter a new line once it has printed Hello World.

Variables

You can declare variables like so:

1
2
3
4
first_number = 1.275
second_number = 2.123
sum = first_number + second_number
print sum

This will look something like this in the irb

1
2
3
4
5
6
7
8
irb(main):003:0> first_number = 1.275
=> 1.275
irb(main):004:0> second_number = 2.123
=> 2.123
irb(main):005:0> sum = first_number + second_number
=> 3.398
irb(main):006:0> print sum 3.398
=> nil

Since everything in ruby is an object you could do something like this:

1
2
3
4
5
6
irb(main):010:0> sum.class
=> Float
irb(main):011:0> 8.class
=> Integer
irb(main):012:0> "Hello World".class
=> String

Arithmetic Operators

Ruby supports all the arithmetic operators that you'd expect any modern programming language to support.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
irb(main):013:0> 3+2
=> 5
irb(main):014:0> 3-2
=> 1
irb(main):015:0> 3*2
=> 6
irb(main):016:0> 3/2
=> 1
irb(main):017:0> 3%2
=> 1

Now you may look at this and wonder how 3/2 is equal to 1? This is because both 3 and 2 here are integers and so ruby performs integer division on them. You can get decimal point answers by adding a .0 at the end of the numbers.

1
2
irb(main):019:0> 3.0/2
=> 1.5

Another thing here is like most programming languages Ruby also struggles in floating point calculation beyond a certain a range for example:

1
2
irb(main):025:0> 1.98765432109876+0.000000000000565
=> 1.9876543210993252

User input

Finally we can get user input through the gets command:

1
2
3
irb(main):026:0> new_number= gets.to_i
1254
=> 1254

Here the gets.to_i takes the input which is in string format and converts it into an integer.

That was Day 1 I guess. I didn't really jump to deep here just tried to go through some basic stuff, like things enough to make a small calculator. I felt there was no need to overdo it and force myself to be learn ruby in 3 days. I guess I'm savoring the experience of learning a new language.