Day 2 here we go!
Constants
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.
|
|
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:
|
|
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
.
|
|
Comments
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 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.