Ruby Programming: How to create a program in Ruby on Rails?
Ruby is a well known Object Oriented Programming Language. For new mates, its difficult to learn Ruby language without any reference. Moreover, There aren’t too many video tuts are available which could help you in learning. So i thought of creating a good, easy and a Descriptive Documentation on Ruby on rails which might help you if you ever wanted to become a ruby on rails developer.
Ruby Programming Syntax:
Q: What is Syntax?
As you guys do lot of works every day. Every Work is done in its proper way. That proper way is known as syntax in Ruby and in all other languages.
Ruby Programming Steps:
Following Steps involves in Creating a Ruby program.
- Writing the Code
- Executing the Program.
Q: What is Program?
Every Software You Use on Your Computer is a program. A simple “Notepad“, “Calculator” and “Paint” etc.
All of these are programs. If you see a few messages in these programs such as
The message written in this Dialogue box
“Do you want to save changes to Untitled?”
was printed on this dialogue box by its programming language’s PRINT Function.
Now One thing to keep in mind is that in all other languages we use different functions whether they are built in functions or user defined, but in ruby we never say function, we pronounce Functions as Method in Ruby.
If this program is written in Ruby Language then that line has been printed due to “puts” or “print” method(As stated above methods are functions in ruby) of Ruby.
What You know now?
- Every thing which you see on screen is printed on screen due to a “Print” function of its language.
- You Know about Syntax. (Proper Format)
What You Don’t Know?
- Source Code
- Object Code
Ruby Programming: Source Code
When you write a program, all of its code is known as source code. WHY?
Because its just a simple Code you have written in simple English.
Ruby Programming: Object Code
The process of Conversion of Human’s written English Source Code into Computers Binary Language is called Compiling.
After Compiling, You can execute that file on your PC which is now known as Object file, and the code of that object file is known as Object Code.
But in Ruby You can’t have an ‘ Object Code ‘ WHY? Read Below
How Do Ruby Works?
When You write the code in Ruby Language, then it needs to be compiled.
The compiler of ‘Ruby Language’ is also known as ‘ruby‘
When ‘ruby‘(compiler) compiles the program if it founds an error it shows error but if no error found then it auto execute the program, it never creates another ‘object code’ file. Now i hope that the concept is getting clearer.
IF YOU HAVE ALL THE ABOVE CONCEPTS CLEARED YOU CAN SEE THE FOLLOWING CODE.
Caution: Don’t read the following code if you haven’t cleared the above concepts. because this will put a burden on your brain. And you might fail to understand it.
# Code started. puts "Hello, World !" print "Hello, World Again !" # Code Ended.
Ruby programming: Code Explanation
- The first line starting with “#” is a COMMENT.
- Second line starting with “puts” is a puts method(Remember methods are functions).
- Third line starting with “print” is a print method(Remember methods are functions).
- Fourth line, like stated in First line that its a comment, This is also a comment.
Comments Won’t Run in Program.
All the commands present in ‘source code‘ executes one by one.
In the present Case:
- puts method will run first.
- Then print method.
Now a little Brief:
If you are using windows, Download and install ruby on your pc in order to create its program.
If you are using linux, then also you must have ruby installed on your distribution.
If you guys are using Kali Linux, then you don’t need to install ruby on it. Because it comes Pre-installed.
Just install Rails on kali linux and done. just run following command to install rails on kali.
$ sudo gem install rails
If everything is set properly, Now Use any of your favourite text editor and write in the above code which i have showed you up with print and puts methods.
Save it somewhere with the extension of ‘.rb‘ i.e. example.rb
What i suggest is that if you wan’t to learn Ruby on Rails Complete then i would recommend you to learn it on linux, Because on windows you might run in some problem and it will be difficult for you to troubleshoot it. if you can than your’e good to go.
So after saving it anywhere,
Windows Users press ‘Windows Key’+’R key’ and then type in ‘cmd’ and hit enter.
Linux Users Open ‘Terminal’
Now Windows users navigate to the directory where you have saved the ‘source code’ file. and then write command
and if there is no error the program will run correctly.
Linux User also do the same thing if you don’t know how to navigate check some linux documentation.
Or Just Follow The Following Guide (Linux)
if you saved the ‘source code’ file on desktop with the name ‘example.rb’
then just type following commands in terminal and the program will run.
$ cd ~/Desktop $ ruby example.rb
puts and print Difference and Detail:
Both will display text on screen.
puts displays text and then starts a new line to execute code on next line.
print display text and then do not start a new line the code keeps on executing on same line.
Code must have to be written in double quotes “”.
Output of Program:
Hello, World ! Hello, World Again !
Now keep on Playing with that code , see you all in next article.
Enough For Today.
Actually i am creating a full course of Ruby and all other popular languages over here.
So Make sure you bookmark my site and Share it with others too.
If you won’t understand any thing feel free to comment.
Make sure to