Ruby Programming/Syntax/Operators
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Operators
[edit] 1. Assignment
Assignment in Ruby is done using the equal operator "=". This is both for variables and objects, but since strings, floats, and integers are actually objects in Ruby, you're always assigning objects.
Examples:
myvar = 'myvar is now this string'
var = 321
dbconn = Mysql::new('localhost','root','password')
Self assignment
x = 1 #=>1 x += x #=>2 x -= x #=>0 x += 4 #=>x was 0 so x= + 4 # x is positive 4 x *= x #=>16 x **= x #=>18446744073709551616 # Raise to the power x /= x #=>1
A frequent question from C and C++ types is "How do you increment a variable? Where are ++ and -- operators?" In Ruby, one should use x+=1 and x-=1 to increment or decrement a variable.
x = 'a' x.succ! #=>"b" : succ! method is defined for String, but not for Integer types
Multiple assignments
Examples:
var1, var2, var3 = 10, 20, 30 puts var1 #=>var1 is now 10 puts var2 #=>var2 is now 20,var3...etc myArray=%w(John Michel Fran Adolf) # %w() can be used as synthetic sugar to simplify array creation var1,var2,var3,var4=*myArray puts var1 #=>John puts var4 #=>Adolf
names,school=myArray,'St. Whatever' names #=>["John", "Michel", "Fran", "Adolf"] school #=>"St. Whatever"
Conditional assignment
x = find_something() #=>nil x ||= "default" #=>"default" : value of x will be replaced with "default", but only if x is nil or false x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false
Operator ||= is a shorthand form of the expression:
x = x || "default"
Operator ||= can be shorthand for code like:
x = "(some fallback value)" if x.nil?
[edit] Scope
Basically in Ruby there's a local scope,a "global" scope and classes scope.
Example:
var=2
4.times do |x| puts x=x*var end #=>0,2,4,6
puts x #=>undefined local variable or method `x' for main:Object (NameError)
This error appears because this x(toplevel) is not the x(local) inside the do..end block the x(local) is a local variable to the block, whereas when trying the puts x(toplevel) we're calling a x variable that is in the top level scope, and since there's not one, Ruby protests.
4.times do |$global| $global=$global*var end #=>0,2,4,6 last assignment of $global is 6 puts $global
This works because prefixing a variable with a dollar sign makes the variable a global. The $ is ugly and the reason behind this is simply to make you not to use or use sparingly these global variables.
4.times do |@instvar| @instvar=@instvar*var end #=>0,2,4,6 last assignment of @instvar is 6 puts @instvar
@instance_variable, instance of what? Of top level, which effectively makes it a global-like $variable
class Test
def initialize(arg1='kiwi')
@instvar=arg1
@@classvar=@instvar+' told you so!!'
localvar=@instvar
end
def print_instvar
puts @instvar
end
def print_localvar
puts @@classvar
puts localvar
end
end
class SubTest < Test
def print_classvar
puts @@classvar
end
end
var=Test.new
var.print_instvar #=>"kiwi", it works because a @instance_var can be accesed inside the class
var.print_localvar #=>undefined local variable or method `localvar' for #<Test:0x2b36208 @instvar="kiwi"> (NameError).
This will print "kiwi told you so!!", then FAIL! with a undefined local variable or method `localvar' for #<Test:0x2b36208 @instvar="kiwi"> (NameError). Why? well, in the scope of the method print_localvar there doesn't exists localvar, it exists in method initialize(until GC kicks it out). On the other hand ,class variables '@@classvar' and '@instvar' are in scope across the entire class and int the case of @@class variables,across the children classes.
class SubSubTest < Test
def print_classvar
puts @@classvar
end
def modify_classvar
@@classvar='kiwi kiwi waaai!!'
end
end
newvar=SubTest.new #newvar is created and it has @@classvar with the same value as the var instance of Test!!
newvar.print_classvar #=>kiwi told you so!!
Class variables have the scope of parent class AND children, these variables are can live across classes, and can be affected by the children actions ;-)
subtest=SubSubTest.new subtest.modify_classvar #lets add a method that modifies the contents of @@classvar in SubSubTest subtest.print_classvar
This new child of Test also has @@clasvar with the original value newvar.print_classvar. The value of @@classvar has been changed to 'kiwi kiwi waaai!!' This shows that @@classvar is "shared" across parent and child classes.
[edit] Logical And
The binary "and" operator will return the logical conjunction of its two operands. It is the same as "&&" but with a lower precedence. Example:
a = 1 b = 2 c = nil puts "yay all my arguments are true" if a and b puts "oh no, one of my argument is false" if a and c
[edit] Logical Or
The binary "or" operator will return the logical disjunction of its two operands. It is the same as "||" but with a lower precedence. Example:
a = nil b = "foo" c = a || b # c is set to "foo" its the same as saying c = (a || b) c = a or b # c is set to nil its the same as saying (c = a) || b which is not what you want.

