Ruby/Variables Datatypes Operators4

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
Ruby Variables, Datatypes, Operators
** Ruby Strings
** More Ruby Strings
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 Strings Operations

[edit] Common strings operations


Finding the length of a string:

x="a string"
y=x.size
puts y

Reversing a string:

x="unreversed string"
y=x.reverse
puts y
Operators

Most of Ruby's operators are method calls.This also means they can be redefined.We will discuss what methods are in a future chapter. In order of precedence, a table with all operators, method calls and non-method calls:

Image:Operators.gif


The non-method call ones:

Image:Non-methods.gif

Most commonly used operatos:

arithmetic operators such as "+" or "-"

logical operators such as "&&" equivalent to "and", the "||" operator equivalent to "or", the "!=" equivalent to "not equal to", or the "<>" equivalent to "different than"

the ternary operator "?" used as a short form of an if statement; use it to test the value of truth of an expression and execute code depending if true or false; (expr) ? (excuteiftrue):(executeiffalse)

Examples of use:

x=1     x is attributed the value of 1
 
x+=2    x is attributed the value of x + 2
 
x<>y    x different than y
 
x>=y    x larger or equal than y
 
(x>y) ? (x+=1):(x-=1) test if x larger than y, if true add 1 to x, if false subtract 1 from x

Previous Next