Methods Flashcards
.gsub!
Global Substitution
example_string.gsub!(/x/, “y”)
.include?
Evaluates to true if it finds what it’s looking for and false otherwise.
string interpolation
example_string = "Andy" print "Hello, my name is #{example_string}" # ==> "Hello, my name is Andy"
while
loop method
example:
i = 1
while i
until
loop method
a complement to the while loop method.
a condition is set in a ‘until loop’ that breaks the loop
example: i = 0 until i == 6 i += 1 end
assignment operators
+= , -= , *= , /= ,
example:
example_variable += 1
You’re telling Ruby: “Add 1 to ‘example_variable’, then assign that new value back to ‘example_variable’.” This provides a succinct way of updating variable values in our programs.
for
loop method for when you do know how many times you’ll be looping
example:
for example_variable in 1…10
Inclusive and Exclusive Ranges
You saw a bit of new syntax in the previous exercise:
for num in 1…10
What this says to Ruby is: “For the variable num in the range 1 to 10, do the following.” The following was to print “#{num}”, so as num took on the values of 1 to 9, one at a time, those values were printed to the console.
The reason Ruby counted to 9 and not 10 was because we used three dots in the range; this tells Ruby to exclude the final number in the count: for num in 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.
iterator
A Ruby method that repeatedly invokes a block of code. The code block is just the bit that contains the instructions to be repeated.
The simplest iterator is the ‘loop’ method.
{}
do (to open the block)
example: i = 0 loop do i += 1 print "#{i}" break if i > 5 end
end
to close the block
example: i = 0 loop do i += 1 print "#{i}" break if i > 5 end
break
breaks a loop as soon as its condition is met.
example: i = 0 loop do i += 1 print "#{i}" break if i > 5 end
next
can be used to skip over certain steps in the loop.
example: for i in 1..5 next if i % 2 == 0 print i end
- In the above example, we loop through the range of 1 through 5, assigning each number to i in turn.
- If the remainder of i / 2 is zero, we go to the next iteration of the loop.
- Then we print the value of i. This line only prints out 1, 3, and 5 because of the previous line.
array
a method where we can store multiple numbers or objects in a variable.
example:
my_array = [1, 2, 3, 4, 5]
.each
A method which can apply an expression to each element of an object, one at a time. The syntax looks like this:
object. each do |item| # Do something
- The variable name between | | can be anything you like: it’s just a placeholder for each element of the object you’re using .each on.
.times
a method thats like a super compact ‘for loop’: it can perform a task on each item in an object a specified number of times. The syntax looks like this:
10.times { print “Chunky bacon!” }
.split
a method to divide the user’s input into individual words.
it takes in a string and returns an array.
multidimensional array
array of an array
example:
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
hash
a collection of key-value pairs.
example:
hash = { key1 => value1, key2 => value2, key3 => value3 }
Values are assigned to keys using =>. You can use any Ruby object for a key or value.
literal notation hash
literally describes what you want in the hash: you give it a name and you set it equal to a bunch of key => value pairs inside curly braces. my_hash = { "name" => "Eric", "age" => 26, "hungry?" => true }
puts my_hash[“name”]
puts my_hash[“age”]
puts my_hash[“hungry?”]
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 ({}).
example:
my_hash = Hash.new
splat arguments
arguments preceded by a *, which signals to Ruby: “Hey Ruby, I don’t know how many arguments there are about to be, but it could be more than one.”
example:
def what_up(greeting, *bros)
return
Sometimes we don’t just want a method to print something to the console, but we actually want that method to hand us (or another method!) back a value.
example:
blocks
You can think of blocks as a way of creating methods that don’t have a name.
Blocks can be defined with either the keywords do and end or with curly braces ({}).
Abstraction / Abstracting
an important idea in computer science, and you can think of it as meaning “making something simpler.”
.sort!
.sort! sorts an array in-place (that is, it destroys the original array and replaces it with the sorted version).
combined comparison operator
It returns 0 if the first operand (item to be compared) equals the second, 1 if first operand is greater than the second, and -1 if the first operand is less than the second.
.sort
returns a sorted array while leaving the original array alone.
.to_sym
converts strings to symbols
example:
“example”
“example”.to_sym => :example
.intern
to internalize.
another way to convert strings to symbols
.select
to filter a hash for values that meet a certain criteria