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





































  

No comments:

Post a Comment