Friday 19 December 2008

Ruby operators et el

Table of Content

= 1

== 3

!= 3

=~ 3

!~ 3

< 3

> 3

<= 3

>= 4

eql? 4

equal? 4

%q 4

%Q 4

#{expr} 4

= <<delimiter 4

.. 4

... 5

<=> 5

=== 5

=

Assignment operator, it assigns values on the right side( rValue) to the variable in the left side(lValue).
a = 1
a = b + c
x = Array.new
a = b = c = 1 is equivalent to a= 1, be = 1, and c = 1. i.e. assignment can be chained.
a = (b = 4+2) + 3 »
b = 6, a = 9
Parallel assignment: a, b = 3, 4 » a= 3, b = 4.

Simple sequence of variables on the left hand side gets assigned to corresponding value from the right hand side. Parallel assignment is pretty useful in some scenario. Let’s say a= 2 and b = 3, Now if you want to swap the values, you can do it single parallel assignment as follows:

a, b = 2, 3 » a = 2, b = 3

a, b = b, a » a = 3, b = 2

Splitting Array into multiple variable: using parallel assignment an array elements can be assigned to a list of variables.

a=[1, 2, 3, 4, 5]

b, c = a » b = 1, c = 2

b, *c = a » b = 1, c= [2, 3, 4, 5]

b, c = 123, a » b = 123, c = [1,2,3,4,5]

b, *c = 123, a » b = 123, c= [[1,2,3,4,5]]

b, c = 123, *a » b = 123, c = 1

b, *c = 123, *a » b = 123, c = [1,2,3,4,5]

Nested assignment: The left-hand side of an assignment may contain a parenthesized list of terms. Ruby treats these terms as if they were a nested assignment statement. It extracts out the corresponding rvalue, assigning it to the parenthesized terms, before continuing with the higher-level assignment.

b, (c, d), e = 1,2,3,4 » b = 1, c = 2, d = nil, e = 3

b, (c, d), e = [1,2,3,4] » b = 1, c = 2, d = nil, e = 3

b, (c, d), e = 1,[2,3],4 » b = 1, c = 2, d = 3, e = 4

b, (c, d), e = 1,[2,3,4],5 » b = 1, c = 2, d = 3, e = 5

b, (c,*d), e = 1,[2,3,4],5 » b = 1, c = 2, d = [3, 4], e = 5

==

Equality operator. It compares lvalue with the rvalue and result true if they same false otherwise. This compares value only.

a, b = 2, 3

a == b »false

c = d= 4

c == d »true

e, f= 4, 4.0

e == f »true :Notice, although f is a floating point number the comparison return true as their value same.

!=

=~

!~

<

>

<=

>=

eql?

equal?

%q

General purpose single quoted string

%q/something as a string/ » something as a string

%Q

General purpose double quoted string

%Q{something as a string} »something as a string

%QString with expression #{50*6} »String with replacement 300

#{expr}

A Ruby expression block. This specific sequence used in string to create or format a string, generate value dynamically. It simply implies that the block of code inside the curly braces will get executed and the result will be embedded into the string. This sequence only can be used in double quoted string.

"10 inch = #{ 10*2.54} cm" »10 inch = 25.4 cm

"#{'Ho! '*3} Merry Christmas" »Ho! Ho! Ho! Merry Christmas

= <<delimiter

This is special marker called document to write long text. Anything you write after the document until the delimiter is found is assigned to the string variable.

aString = <<END_OF_STRING

The body of the string

is the input lines up to

one ending with the same

text that followed the '<<'

END_OF_STRING »" The body of the string\n is the input lines up to\n one ending with the same\n text that followed the '<<'\n"

..

Range operator; syntax is m..n implies from m up to n , m and n inclusive. m/n could be number, alphabet or any object that implements range.

1..4 » 1, 2, 3, 4

'a'..'k' » a, b, c, d, e, f, g, h, i, j, k

...

Range operator; syntax is m...n implies from m up to n , m included but not n. m/n could be number, alphabet or any object that implements range.

1...4 » 1, 2, 3

'a'...'k' » a, b, c, d, e, f, g, h, i, j

<=>

Refer to spaceship operator

===

Case equality operator. Used to test equality within a when clause of a case statement. All Ruby class implements this operator. In most cases this implies if the value falls within the range. But it can vary depending on the implementation of the class.

(1..10) === 3 » true

(1..10) ===11 » false

kind = case year

when 1850..1889 then "Blues"

when 1890..1909 then "Ragtime"

when 1910..1929 then "New Orleans Jazz"

when 1930..1939 then "Swing"

when 1940..1950 then "Bebop"

else "Jazz"

end

In the above example === operator used in every when clause to identify the match. Simply comparison === target

No comments:

Post a Comment