FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[TUT] Introduction to ruby programing [5 Stages]





pirate
Ruby Programming


Hello and welcome ton an introduction to ruby programming, hope you find this helpful.


Hello World
Code:

print "Hello world"

puts 'Hello world'

This is the most simple program in ruby a simple hello world, the 2 ways im showing you is print and puts, they both do the same but puts is one letter shorter xD.


Variables

Code:

variable = 10
puts ("variable = #{variable}")

This is a very simple Look at variables, A variable is a name that can hold one single value. In this case we called ours variable To print out the value of a variable we have to encode it into our string. We do this by using #{} the variable name is placed with in the {}.


User Input

To show how the user input works, we are going to make a simple name program.

Code:

puts 'What is your name??'
name = gets
puts ("Your name is #{name}")

To add user input all we have done is used the gets statement. This means that we can accept user input and then store that input to the variable name, we have then printed it out on the following line. Simplesss.


If Statement

The If statement allows us to make a program that tests if a condition is true or false.

Code:

variable = 10
if (variable == 10) then
puts 'you have 10'
end

In this code we are checking to see if the condition is true. To do this we have said "if (variable == 10)" meaning if variable is exactly equal to 10 then print you have ten. If it isn't 10 then it wont print anything. You have to use == because you are comparing two items.

Else, Elsif

This is an extension on the if statement.

Code:

variable = 10
if (variable == 10) then
puts 'you have 10'
elsif (variable == 11)
puts 'you have 11'
else
puts 'wrong number'
end

This code is an extension on the previous, it is just allowing us to have more than one option before the condition is declared false, meaning we have a number of options. It really speaks for it's self in the fact that the statement are actual words and so you can almost read it like a book.



Thank you for reading, all this was is a quick intro two a few of the commands and statements that you can use in ruby. For more detail you could visit my website or my youtube channel where each of these has it's own programming video.

http://www.youtube.com/watch?v=x6RB2k_PthM[/code][/u]
underscore
good tutorial mate keep it up
jajarvin
I just started to learn Ruby language.
Once again a good starting point was Firhost Forum and I found this tutorial.
Now I already can do some coding with Ruby.Wink
Related topics
Sleep less to get more out of life?
Join here and get 50frih$ and get paid for each post 5 frih$
Programming References and Online Books
Give FRIH$ 5 and get 1 gmail invitation
WHat is the best PHP guidebook?
INTRODUCTION AU LANGAGE C /C++
Is philosophy a science?
5 Sistemas Libres Para Montar tus Redes Sociales
King Tut's Curse
Ruby on Rails v/s Python in Django
5 Things We Love/Hate About Windows 8.
how to install ruby on rails on windows
<hr> stilosi con i CSS
Install Mambo 4.5.2 And Vbulletin 3.0.7
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.