Navigation Menu

Approach to Ruby Programming



What is Ruby?


Ruby is a Object Oriented Programming language. Also it is a  scripting langauge. It is a general purpose interpreted programming language.

It has features similar to python, Perl as ruby also a server side scripting langauge. It can be embedded into html. Used to develop internet and intranet applications.


Ruby Environment Setup

Step1: Download Ruby.















Step2: Install the downloaded file.









Step3: Open command prompt and enter command "gem install gosu".



Writing a First Perl Program


Step1: Write the starting program in the document.




Step2: Run the document using command prompt by giving total folder path.




Creating Class


Creating Object




Creating Variables

Ruby variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable.


Example:




Output:




Array Variables

Array variable is prefixed with '@' operator and populated using parenthesis or qw operator.

Example:






Global Variable

Global variable declared by using '@' symbol.






Instance Variable

Instance Variables are object related and only accessed through object.








Conditional Statements


1. if..else() statement:



Output:



Looping Statements

1. for() loop:






2. while() loop:



3. until() loop:






4. ruby next:

    next statement will stop the execution of loop when condition satisfies.





String Manipulations

1. Creating String Object:






2. Concatination:




2. Finding a Substring:





3. Index():

   This function returns 'nil' if no match found or else returns position of matching string.





Interacting to File Systems

Class File provides all functions required for file operations like puts, gets, open, close, readlines() etc.

Example1: Opening and reading from a file.

   Step1: Input file




   Step2: Program and Output






Example2: Searching and displaying text from a file.


    Step1: Input file



    Step2: Searching and displaying line














0 comments: