-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp002rubynumbers.rb
More file actions
57 lines (46 loc) · 1.04 KB
/
p002rubynumbers.rb
File metadata and controls
57 lines (46 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#p002rubynumbers.rb
=begin
Ruby Numbers
Usual Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
=end
puts 1+2
puts 2*3
#integer division
#When you do arithmatic with integers, you'll get integer answers
puts 3/2
puts 10-11
puts 1.5/2.6
#Modulus Operator
puts (5 % 3)
puts (-5 % 3)
puts (5 % -3)
puts (-5 % -3)
# Difference Between or and ||
puts nil || 2013
puts false || 2013
puts "ruby" || 2013
#Assigning Value with OR for defaults
#The Long Way
@variable = @variable || "Default Value"
puts @variable
#wont set following variable unless nil
@variable = nil
#The Shortcut
@variable ||= "Another Default"
puts @variable
#going to write a complex expression
def g *args #The Splat here says I accept 1 or more arguments, in the form of an array
args #This returns an array
end
def f arg
arg
end
x,y,z = [true,'two',false] #parrallel assignments lets us do this
if a = f(x) and b=f(y) and c=f(z) then
d = g(a,b,c) #An array is returned, and stored in variable d
end
p d #using p to puts and inspect d