Go Back

Getting to Know Ruby: Day 2

Published on: August 21, 2020 | Reading Time: 2 min


Day 2 here we go!


Constants in ruby begin with a capital or upper case letter. Unlike some programming languages ruby allows users to change constants after declaration except it will give you a warning for it.

 irb(main):001:0> Constant = 78.1
=> 78.1
irb(main):002:0> Constant = 54.9
(irb):2: warning: already initialized constant Constant
(irb):1: warning: previous definition of Constant was here
=> 54.9
irb(main):003:0> puts Constant
=> nil

Here as you can see Ruby gave us a warning when we tried to change the constant 'Constant' but let us change it nonetheless.

File Operations

In ruby we do file operations using the File object. Consider the following code:

irb(main):001:0> file_writer = File.new("helloworld.org", "w")
=> #<File:helloworld.org>
irb(main):002:0> file_writer.puts("* Hello World").to_s
=> ""
irb(main):003:0> file_writer.close
=> nil

Here in the first line we're opening a file hellowworld.org using the File object and "w" parameter tells ruby that we want to write to it. Next we use puts to put things into the file, here which is * Hello World, only after converting it into a string of course using .to_s. After we're done writing into the file we close the file using writer.close

Now let's read from that file. To do that we can use File.read.

irb(main):004:0> file_reader = File.read("helloworld.org")
=> "* Hello World\n"


Commenting in ruby is pretty straight forward and honestly in all the languages I've seen my most favorite yet. Single line comments is done by beginning the line with a # which is nothing special, it's milti-line comments that really caught my eye, you begin a multi-line comment with =begin and end it with =end. I don't know something about this feels very natural to me. I like it very much.

# this is a single line comment
is a

This have been pretty straight forward up until now. Tomorrow I'll probably get started with conditionals. I still have a lot to cover but I'm getting there. After conditionals I might go to loops, all of them. At some point I will have to look into OOP with Ruby, that is going to be one long post maybe I'll break it into smaller posts. Maybe this might be the last post I make about Ruby. Who knows what the future holds.