Friday, 10 October 2014

The Structure & Execution of Ruby Programs

1. Lexical Structure
     1) Ruby interpreter parses a program as a sequence of tokens
     2) Tokens include comments, literals, punctuation, identifiers & keywords
          1] Literals
              - values that appear directly in Ruby source code
              - include numbers, strings of text & regular expressions
              - e.g. 1        # An integer literal
                       1.0     # A floating point literal
          2] Identifiers
              - simply name
              - Ruby uses identifiers to name variables, methods, classes etc
              - consists of letters, numbers & underscore characters but may not begin with a number & may not include whitespace
              - Ruby is case sensitive language, Lowercase letters & uppercase letters are distinct
         3] Whitespace
             - Space, tabs & newlines
             - Semicolon can be used to terminate statements but only required if more than one statement on same line.

2. File Structure
    1) Shebang Comment 
        - tell the operating system how to execute it
        - If program contains this comment then it must appear on first line
    2) Coding Comment
        - If program contains this comment then it must appear appear after Shebang
    e.g
    #!/usr/bin/ruby -w            shebang comment
    # -*- coding: utf-8 -*-     coding comment
    require 'socket'                load networking library
   
    -----                               program code goes here

    _END_                           mark end of code
    ---                                   program data goes here

3. Program Encoding
- By default , the ruby interpreter assumes that Ruby source code is encoded in ASCII

4. Program Execution
- Ruby is a scripting language





































  

Thursday, 9 October 2014

Expressions in Ruby

1. Control structure like If returns value true or false
2. Loops & Method defination returns nil

Ruby Package Management with gem

1. Ruby Package management system is known as RubyGems & packages/modules distributed using RubyGems are called gems
2. Commands
    1) gem install
        - Installs most recent version of requested gem
    2) gem list
        - List installed gems
    3) gem environment
        - Display RubyGems configuration information
    4) gem update rails
        - Update a named gem
    5) gem update
        - Update all installed gems
    6) gem update --system
        - Update RubyGems itself
    7) gem uninstall rails
        - Remove an installed gem

String in Ruby

1. Strings are mutable.
2. freeze method is used to prevent any future modifications to object
3. String Interpolation - variables substituted into string. #{}

Range in Ruby

Range represents values(usually integers) between two end points
e.g.
  1..3      # All x where 1 <= x <= 3
  1...3     # All x where 1 <= x < 3

Regexp in Ruby

A Regexp (regular expression) object describes a textual pattern & has methods for determining whether a given string matches that pattern or not
e.g. /[Rr]uby/ # Matches 'Ruby' or 'ruby'

Variables in Ruby

1. Global variable - prefixed with $
2. Instance variable - prefixed with @
3. Class variable - prefixed with @@