Tuesday, February 19, 2008

Basics of Ruby

Today I learnt the basics of Ruby...

A program written in Ruby will have a file extension with .rb(not mandatory)

Ruby program execution goes from Top-Down approach

Syntax rules in Ruby...

1.Free format
2.Case Sensitive ( AB and ab or not the same..)
3. Single line comment can be given using #, Multiple line comment using =begin and = end

Example 1(using 1- line comment) :

puts " Leader" # This is a Ruby code which outputs "Leader"

Example 2 (Using multiple line comment)

puts "Leader"

=begin
'puts' means put a string
It outputs the string in the quotes
=end

4. No delimeter for a single line.....but should use semicolon(;) for multiple statements in one line

Example 1 : puts "Leader" output : Leader

Example2 : puts "Gandhi"; puts "is a great Leader" output : Gandhi is a great Leader

Linefeed is treated like a semicolon
If a line ends with a backslash (\), the linefeed following it is ignored;
this allows us to have a single logical line that spans several lines

5. Keywords

There are around 38 keywords in Ruby.These are reserved and cannot be used for other purposes

Keywords in Ruby


  1. alias

  2. and

  3. BEGIN

  4. begin

  5. break

  6. case

  7. class

  8. def

  9. defined

  10. do

  11. else

  12. elsif

  13. END

  14. end

  15. ensure

  16. false

  17. for

  18. if

  19. in

  20. module

  21. next

  22. nil

  23. not

  24. or

  25. redo

  26. rescue

  27. retry

  28. return

  29. self

  30. super

  31. then

  32. true

  33. undef

  34. unless

  35. until

  36. when

  37. while

  38. yield


Note : False and Nil are reserved words in Ruby....so they can't be used as(Zero)

No comments: