. View this ad or post your own Ruby Job!

Wednesday, October 26, 2005

Ruby Nuts and Bolts

What have I found out so far? Now, let us explore some of the features of Ruby.

  1. Free format
  2. Case sensitive
  3. Comments - Anything following an unquoted #, to the end of the line on which it appears, is ignored by the interpreter. Also, to facilitate large comment blocks, the ruby interpreter also ignores anything between a line starting with "=begin" and another line starting with "=end"
  4. Statement delimiters - Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line; a linefeed is treated like a semicolon. If a line ends with a backslash (\), the linefeed following it is ignored; this allows you to have a single logical line that spans several lines
  5. Names in Ruby - Ruby names are used to refer to constants, variables, methods, classes, and modules (more of this later). The first character of a name helps Ruby to distinguish its intended use. Certain names, are reserved words and should not be used as variable, method, class, or module names. Lowercase letter means the characters ''a'' though ''z'', as well as ''_'', the underscore. Uppercase letter means ''A'' though ''Z,'' and digit means ''0'' through ''9.'' Name characters means any combination of upper- and lowercase letters and digits
  6. Variables - Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations. Variable name itself denotes its scope (local, global, instance, etc.).
    1. A local variable name consists of a lowercase letter followed by name characters (sunil, myCount, _z, hit_and_run).
    2. An instance variable name starts with an ''at'' sign (''@'') followed by an upper- or lowercase letter, optionally followed by name characters (@sign, @_, @Counter).
    3. A class variable name starts with two ''at'' signs (''@@'') followed by an upper- or lowercase letter. optionally followed by name characters (@@sign, @@_, @@Counter).
    4. A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout (module MyMath, PI=3.1416, class MyPune).
    5. Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x).
  7. Method names should begin with a lowercase letter. ''?'' and ''!'' are the only weird characters allowed as method name suffixes (More on this later).
  8. Strings have been explored in the program that follows:
=begin
Exploring Ruby
My first Ruby program!
=end

puts 1 + 2
puts 2 * 3
# Integer division
puts 3 / 2
puts 10 - 11
puts 1.5 / 2.6
puts "Hello World"
# Can use " or ' for Strings, but ' is more efficient
puts 'Hello World'
# Escape sequence
puts 'It\'s my Ruby'
# New here, displays the string three times
puts 'Hello' * 3
# String concatenation
puts "Hello" + "World"
# Defining a constant
PI = 3.1416
puts PI
# Defining a local variable
myString = "I love my city, Pune"
puts myString
=begin
Conversions
.to_i, .to_f, .to_s
=end
var1 = 5;
var2 = '2'
puts var1 + var2.to_i
It's to be noted that any given variable can at different times hold references to objects of many different types. A Ruby constant is also a reference to an object. Constants are created when they are first assigned to (normally in a class or module definition; they should not be defined in a method - more of this later). Ruby lets you alter the value of a constant, although this will generate a warning message.


More next time.


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

0 Comments:

Post a Comment

<< Home

Valid XHTML 1.0!