HackToHell. Powered by Blogger.

Learn Ruby easily Part II

For Part I and an introduction to ruby - check this out.

I told you how you can print stuff using the print method of ruby to print stuff now let's get an input from the user.
Code:

print( 'Enter your name: ' )
name = gets()
puts( "Hello #{name}" )

http://codepad.org/bEE666kx
Run this and it will ask for your name and when you enter it, it will show Hello [your_name]

You must have noticed that in order to use a variable in puts , along with string , we use #{var_name}
If we use #{var_name} then we MUST use " " (double quotes)

Commenting in Ruby


In ruby for a single line comment use #
E.G:-
print("Hai World") #This is a comment

For multiline comment use

=beign
This is comment
section.All are
comments here
=end

IF ...ElSE NO! IF THEN .... ELSE


If you have programmed in QWBASIC or GWBASIC, you will know about these.
Usage.
nam = 10 * 4
num= 11 *4
if(nam > num)
then
print('nam is greater')
else
print ('num is greater')

Global and Local Variables


What are global and local variables, global variables are global i.e only one copy exists and as for instance variables, they are in methods ONLY!.
How to define global variables?
they are started with dollar sign $bitly $hthtb These are global vars.

How to define local variables?
they start with @, e.g:- @hacktohell @learnruby

Some books to help you
     
Share on Google Plus

About hacktohell

Love technology.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment