Ruby Operators

Operators

single column :  used to assign a symbol to a string , example :key
(symbol can be stringified as string like that :foo.to_s => “foo”)
see more here (StockOverflow).

comparison : For boolean expressions, always use && and || instead. For flow control, use if and unless; (Rule of thumb: If you have to use outer parentheses, you are using the wrong operators.)

more about boolean operations here

Question marks

methods that return boolean values end in a question mark, examples:
.valid? .even?
? along with a character, will return the ASCII character code for A
ternary operator ?? :

Ternary operator can shorten your if/then statements

test-expression ? if-true-expression : if-false-expression
score > 40 ? 'Pass' : 'Fail'
Enumerable – see all at Ruby Doc: String

 

Examples

 

Example 1

 

    • arr=[]

 

    • arr=new(size, default)

 

    • arr=[number, ‘string’, :symbol]

 

 

Iterating through arrays

indexes starts at 0, negative index start from the end

.each method (array unchanged) arr.each { |e| puts e } => puts a[0], a[1], etc…

 

 


 

Example2

 

a=[]

Tip:


Example3

see discussion in Ruby Cook Book


More Examples: