-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path1-basics.rb
133 lines (96 loc) · 2.14 KB
/
1-basics.rb
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# =================
# Intro to Ruby
# =================
puts "Hello World!"
# =================
# types
# =================
# String
"a quick brown fox jumps over the lazy dog"
# Symbol
:name
# Fixnum (integer)
42
# Float
3.14
# TrueClass
true
# FalseClass
false
# NilClass (yes, even nil is a class!)
nil
# =================
# looks like a duck,
# quacks like a duck
# =================
x = "a string" # x.class => String
x = 1 # x.class => Fixnum
x = 2.3 # x.class => Float
x = true # x.class => TrueClass
x = false # x.class => FalseClass
x = nil # x.class => NilClass
# =================
# array
# =================
countries = ["canada", "brazil", "spain"]
# =================
# hash
# =================
person = { :name => "Brock Whitten", :location => "West End" }
# =================
# collection (array of hashes)
# =================
people = [
{ :name => "Brock Whitten", :location => "West End" },
{ :name => "Brian Leroux", :location => "Gastown" },
{ :name => "Thurston Moore", :location => "NYC" }
]
# =================
# methods
# =================
# a simple method
#
def hello
puts "hello world!"
end
# with an argument
#
def hello(place)
puts "hello #{place}!"
end
# add a default to the argument
#
def hello(place="world")
puts "hello #{place}!"
end
# add a default to the argument
#
def hello(place="world", *args)
#...
end
# =================
# blocks
# =================
3.times do
puts "hello!"
end
# lets iterate over an array
#
countries.each do |country|
puts "hello #{country}"
end
# lets iterate over the hash
#
person.each do |key, value|
puts "the value of #{key} is #{value}"
end
# this is also a block...
#
countries.each { |country| puts country.capitalize }
# =================
# ranges
# =================
cold_war = 1945..1989
alphabet = "a".."z"
cold_war.include?(1980) # => true
alphabet.each {|l| print l } # => abcdefghijklmnopqrstuvwxyz