-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_commands
204 lines (144 loc) · 5.23 KB
/
basic_commands
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
print "Oxnard Montalvo"
The print command just takes whatever you give it and prints it to the screen.
puts "What's up?"
puts (for "put string") is slightly different: it adds a new (blank) line after the thing you want it to print.
#=====================================
.length
outputs length of the string
"I love espresso".length
.reverse
outputs reversed order version of the string
"String".reverse
.upcase
.downcase
.capitalize
capitalizes the first letter of a string and makes the rest of the letters lower case.
! at the end of capitalize. This modifies the value contained within the variable answer itself. The next time you use the variable answer you will get the results of answer.capitalize
.sort!
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
# To sort our books in ascending order
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
# Sort your books in descending order
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }
.object_id
#method gets the ID of an object—it's how Ruby knows whether two objects are the exact same object.
:sasquatch.to_s
# ==> "sasquatch"
"sasquatch".to_sym
# ==> :sasquatch
.each_key
.each_value
my_hash = { one: 1, two: 2, three: 3 }
my_hash.each_key { |k| print k, " " }
# ==> one two three
my_hash.each_value { |v| print v, " " }
# ==> 1 2 3
.to_i will convert a string to an integer.
.to_sym will convert a string to a symbol
.to_s will convert to a string
.upto
.downto
"A".upto("Z") { |letter| puts letter }
.collect #takes a block and applies the expression in the block to every element in an array.
.floor method rounds a float (a number with a decimal) down to the nearest integer.
#=====================================
.respond_to? takes a symbol and returns true if an object can receive that method and false otherwise
.push can be replaced by <<, the concatenation operator (also known as "the shovel")
drink = "espresso"
"I love " + drink
# ==> I love espresso
"I love " << drink
# ==> I love espresso
#=====================================
==
is equal to
!=
is not equal to
#=====================================
logical or boolean operators:
Ruby has three:
and (&&), or (||), and not (!)
Ruby's || is called an inclusive or because it evaluates to true when one or the other or both expressions are true.
assignment operators
=, +=, -=, *=, and /=
conditional assignment operator is made up of the or (||) logical operator and the normal = assignment operator.
||=
.include?
method which evaluates to true if it finds what it's looking for and false otherwise.
.gsub!
method which stands for global substitution.
The syntax looks like this:
string_to_change.gsub!(/s/, "th")
/s/ has to be between slashes instead of between quotes. Note: you cannot put a space between gsub! and the bit in parentheses.
#=====================================
1...10 means "go up to but don't include 10."
If we use two dots, this tells Ruby to include the highest number in the range.
#=====================================
.split; it takes in a string and returns an array. If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text, called a delimiter. For example,
text.split(",")
#=====================================
ARRAYS
array = [5, 7, 9, 2, 0]
array[2]
# returns "9", since "9"
# is at index 2
=begin
This is called access by index.
Arrays of arrays are called multidimensional arrays.
=end
#=====================================
HASHES
a hash is a collection of key-value pairs.
Hash literal notation looks like this:
hash = {
key1 => value1,
key2 => value2,
key3 => value3
}
Shortcut:
my_hash = Hash.new
Setting a variable equal to Hash.new creates a new, empty hash; it's the same as setting the variable equal to empty curly braces ({}).
pets = Hash.new
pets["Stevie"] = "cat"
# Adds the key "Stevie" with the
# value "cat" to the hash
.delete
#Ruby makes it easy to remove a movie/rating pair from our hash: we just write movies.delete(title)!
#=====================================
.to_s
converts the value from a number to a string.
.sort_by function returns an array of arrays:
colors = {"blue" => 3, "green" => 1, "red" => 2}
colors = colors.sort_by do |color, count|
count
end
colors.reverse!
#=====================================
SPLAT
def what_up(greeting, *bros)
bros.each { |bro| puts "#{greeting}, #{bro}!" }
end
what_up("What up", "Justin", "Ben", "Kevin Sorbo", "rudy", "julie")
#=====================================
# I'm a comment!
=begin
I'm a multiline comment!
I don't need any # symbols.
Remember begin and end must be on their own lines.
=end
#=====================================
# method that capitalizes first letter of a word
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
#=====================================
def alphabetize(arr, rev=false)
end
rev=false tells Ruby that alphabetize has a second parameter, rev (for "reverse") that will default to false if the user doesn't type in two arguments.
#=====================================
for num in (1..n)
prime_array.push(prime.next)
end
#ABOVE IS SAME AS BELOW
n.times {prime_array.push(prime.next) }
#=====================================