Ruby Introductory Examples

Hello World

   1. # p001hello.rb  
   2. puts 'Hello'  
   3. p 'hello'  
   4. print 'hello'

Numbers

   1. # p002rubynumbers.rb  
   2. =begin  
   3.  Ruby Numbers  
   4.  Usual operators:  
   5.  + addition  
   6.  - subtraction  
   7.  * multiplication  
   8.  / division  
   9. =end  
  10.   
  11. puts 1 + 2  
  12. puts 2 * 3  
  13. # Integer division  
  14. # When you do arithmetic with integers, you'll get integer answers  
  15. puts 3 / 2  
  16. puts 10 - 11  
  17. puts 1.5 / 2.6

Strings

=begin  
 Ruby Strings  
 In Ruby, strings are mutable  
=end  
 
puts "Hello World"  
# Can use " or ' for Strings, but ' is more efficient  
puts 'Hello World'  
# String concatenation  
puts 'I like' + ' Ruby'  
# Escape sequence  
puts 'It\'s my Ruby'  
# New here, displays the string three times  
puts 'Hello' * 3  
# Defining a constant  
PI = 3.1416  
# puts PI
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.