Introduction
In this part, we will look into some basic things that every programming language should contain, means to get the output in the console as Hello World! we have to know about the print function right? And one good suggestion that I got and still always try to follow is TO BUILD STRONG FUNDAMENTALS.
Variable
So, the actual meaning of the variable is "not staying the same". The same meaning applies to programming also. a location that holds data to be used in the programs. It makes a lot of sense when we make our hands dirty with them.
number = 12
name = "RUBY"
# In the above 2 lines "number" and "name" are the varibles
As it is a programming language it has some rules and we can't call everything which was on the left side as variables. Let's look into the rules for declaring the variable.
Valid
A variable name must start with the alphanumeric or underscore only. We have some special variables bare with we will discuss them after.
puts name1 = "Malavi" #Malavi
puts name_2 = "Molavi" #Molavi
Stay Here !!!
what's puts in the above lines, was simple in the ruby we use it to display something on the console we can also use print but there is a difference between both. we will look at that also.
Invalid
- Variable names can't start with a capital letter. Because if a variable start with the capital letter ruby considers it a constant it also stores the data but the can't be changed. Not an invalid but we can imagine it as an exception.
#Note code explanation
Name = "Malavi" #variable name start with capital letter
puts Name #Malavi (output)
Name = "Hashnode" # we are trying to change the varible value
puts Name
#warning already initialized with Malavi
I think it is making some sense !!! It's partially come under an invalid declaration
It can't start with any numeric values
puts 1name = "Malavi" #invalid
Do not start with any special characters
puts +name = "Malavi" #invalid
Note: There is an exception by placing @, $ and @@ in front of variable names. These are the special variables.
Special variables
The special variable in Ruby is also called sigils. They play a very important role in the scoping we will at that scoping concept in upcoming blogs. if place $ and @ in front of any variable we can call it a special variable.
Data Types
Come on guys!! it was pretty self-explanatory it has the same data types like
Here is the point why have to learn types of data. So, the variable does not store everything that you want to place. They only store certain data types. This is why they come into the picture.
They only store
Numeric: integer, float, Complex, big decimal, Rational.
String: It is a collection of characters that are placed in between simple(' ')or double quotes (" " ).
Boolean: true or false.
Note: Complex data types can be defined in a different way using a complex function in Ruby
complex_dec = complex(2,3) # like this
Puts & Print
In Ruby, we use these two commands to display something on the screen then, why two?? Yes, this is a great question even though the function of both is the same but there is a difference in displaying.
print: It just takes whatever you give it & prints it on the screen.
puts: It adds a new(black) line after the thing you want it to print.
We can understand this clearly with the help of code
print "Malavi"
print "Pande"
puts "Malavi"
puts "Pande"
#output
MalaviPande #This is because of two print statements
Malavi #first puts
Pande #second puts
In addition to these, I want to let know a simple concept that plays a vital role and life a saver of a lot of developers nothing but "comments"
Comments
These are also part of the code but not executed by the interpreter.
single-line comment: The statement which has # in front of it is considered as this.
Multi-line comments: In my opinion, the syntax is so easy to remember and simple as well.
#single line comment
=begin
This is the
multi-line comment
=end
Conclusion
This is the end of the blog readers. Hope you learn something if you want to know my previous blog click the below link. DO feel free to share your thoughts. If you find it a useful share. Thankyou again my fellow learners. Happy coding & hustling.