Ruby/Ruby Basics

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
** Writing Programs in Ruby
** Ruby and the Text Editors
** Escape Character Sequence
** Ruby Arithmetic
** Comments in Ruby
** Ruby Documentation
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby Basics

In this section of the tutorial we will have a brief understanding of the basic fundamentals of Ruby programming. we will have a quick tour on the Commnad line options, The various types of Text editors which can be used with Ruby and what are the points which a programmer should take into consideration while making the choice for editor.

This section will also cover the aspects of Ruby Symentaics and syntactic style, the way how to implement the comments in ruby.

[edit] Ruby Command Line and its Options

Programmers prefer to run Ruby from the command line like many other scripting language interpreters. We have the facility of providing the name of a program file along with the command-line switches. These command-line switches instruct the interpreter to perform the functions as required by the programmer.

Ruby provides us with the command-line switches which are more than twenty in number.

Let’s discuss few of these commonly implemented switches with their syntaxes and examples:

1. –c: This is implemented to check the syntax of a program file without executing the program.It is mostly implemented with the -w switch as they both when taken together help the developer in checking the syntax and also provides the warning message is reuired to be given to the developer.

 syntax:ruby -c filename.ext
 example: ruby -c abc.rb


2. -w : This switch will provide warning messages during program execution to the developer.

 syntax:ruby -w filename.ext
 example:ruby -w abs.rb


3. -e : It executes the actual code provided in quotation marks on the command line instead of code in the file.

  syntax:ruby -e 'instruction to execute"code segment"'
  example:ruby -e 'puts "Demo Code"'

4. -v : It shows Ruby version information, and execute the program in verbose mode.

  syntax: ruby -v
  example:ruby -v

5. –version: It shows Ruby version information similar to -v commnadline but never executes any code even if it is written after the - version. It just prints the version number anf immediately exits from there.

  syntax:ruby --version
  example:ruby --version

6. Check syntax (-c): The -c switch instructs Ruby to check the code in the file for its accuracy without executing the code.

 syntax:ruby -c filename.ext
 example:ruby -c xyz.rb

7. –a: This command line is used with -n or -p to split each line.

 syntax: ruby -a filename.ext
 example:ruby -a schoollist.ext

8.-C dir: It changes the working directory to given directory before executing.

 syntax:ruby -C directory name
 example:ruby -C 

9.-d: This enables debug mode. This command is very much similar to the -debug command line which also enables the debug mode.

  syntax:ruby -d

10.-e prog : This command line is used to specify prog as the program from the command line.

11.-h: It displays an overview of command-line options.

 syntax:ruby -h

12. -i [ext]: This overwrites the file contents with program output. In this the original file is saved with the extension ext. The original file gets deleted if ext isn't specified by the programmer.

syntax:ruby -i[ext]

13. -I dir: It adds dir as the directory for loading libraries.

syntax:ruby -I dirname

14. –l: It enables automatic line-end processing.

 syntax: ruby -l filename.ext
 example:ruby -l lastname.rb 
 let us say that this file lastname.rb contains four names james willy lyn mathew , 
 without -l in command line will give the output as:
 jameswillylynmathew

 while putting -l in command line will give output as
 james
 willy
 lyn
 mathew

15. -p: This switch is used to put the code within an input loop. It automatically writes $_ for each iteration.

16. -X dir: It changes the directory before executing. It is very similar to -c switch.

syntax:ruby -X directoryname

17. –copyright: It is used to display the copyright notice.

syntax:ruby -copyright

18. —debug: It will enable the debug mode.

syntax:ruby -debug 

19. —help: It will display an overview of command-line options.

syntax: ruby -help

20. —version: It is used to display version.

syntax: ruby -version
Next
Personal tools