Ruby Flashcards
See how many letters in a string
“String”.length = 6
Flip letters backwards
“String”.reverse = gnirtS
Multiplying strings
“String” * 5 = stringstringstringstringstring
Convert an things into a string
40.to_s = 40
Convert things into integers
'5 is the number'.to_i = 5 'The number is 5'.to_i = 0 #to_i ignores the first thing it doesnt understand(and the rest of the string from that point on.) so the first one was converted to 5 but the second, sonce they started with letters, were completely ignored so the computer picked zero.
What are [ ]?
Empty lists. Lists store things in order.
Like a line for popcorn.
Check which one is the highest in an array.
[12, 47, 35].max = 47
What is a variable?
A place where you store programs and save them for later use
Arranging values in order
Ticket.sort
[12,35,47]
What are arrays?
Lists for storing things in order
Converts things into floating point number.
99.to_f = 99.0
Convert things into an array.
.to_a
Convert things into list.
.lines
Target and find things then search and replace.
poem[‘string’]= ‘replacement’
What is chaining?
Combining methods to get a lot more done. #ex. poem.lines.to_a.reverse.join - break up a poem, reverse it, reassemble it.
What is { }?
An empty hash. A little dictionary. Hash is a series of key value.
Hash recipe: variable = {key:value}
Example:
B= {name : antoine}
What are symbols?
\: tiny efficient code words with a colon. #ex. :splendid
What are blocks?
Chunks of code which can be tacked on to many of rubys methods. #ex. books.values.each { |rate| ratings[rate] += 1}
What is def
def means define. Define a method or create one. #ex. def look <<< method name
What is File.foreach?
A method which opens a file and hands each line to the block.
What’s split?
A method for strings, which breaks the string up into an array.
What is strip?
A method that removes extra spaces.
What is attr?
Attributes
What are classes?
Everything in Ruby is some kind of object. Classes explain objects. How a certain object works.
What are accessors?
Variable attached to an object which can be used outside the object.
What are instance variables?
Grants view access to variables with @
What are the string formatting methods?
ljust, center, rjust. #ex. Line_width = 40 str = '--> text Puts(str.ljust( line_width)) Puts(str.center( line_width)) Puts(str.rjust( line_width))
What are the arithmetic methods?
\+ Addition - subtraction * multiplication / division ** exponention or square of a number % modulus or remainder
What is abs?
This method simply returns the absolute value of the number
#ex. puts (5+2).abs = 3 puts (2+5).abs = 3
How does the rand method work?
If you give an integer parameter (by calling rand(5) for example) it will give you an integer greater than or equal to 0 and less than 5 (so five possible numbers from 0 to 4). #ex. puts(rand(100)) = 4
What is srand?
Return same random number in the same sequence on two different runs of your program. It will do the same thing every time you seed it the same number
#ex
srand 1976 #first program
puts(rand(100)) = 50
srand 1976 #second program
puts(rand(100)) = 50
What is the Math object?
Has all the features you would expect a decent calculator to have. #ex. Math::PI Math::E -exponention Math.sin Math.cos Math.tan Math.log Math.sqrt
What are the comparison methods?
> greater than
< less than
== equal to
!= not equal to
What are the logical operators?
&& and if both are true = true otherwise false
| | or if only one is true = true but if both is false then false
! Opposite. !true = false
What is branching?
If what comes after the IF is true we run the code between the IF and the END. If what comes after the IF is false, we dont. Plain and simple. Branching is kind of like coming to a fork in a code. Do we take the path whose name == ‘chris’ or ELSE do we take the other?
What is looping?
Makes the computer do the same thing over and over again.
What is a while loop?
A while loop works kinda like IF. while something is true then keep looping or ELSE do something else or END.
What is recursion?
A method that calls itself just like a loop but doesnt loop forever
What are factorials?
The factorial of an integer is the product of all the integers from itself down to 1. #ex. The factorial of 3 is just 3 times 2 times 1 or 6
What is the recipe for reading a value out of a hash?
Variable[:key] => value
Example
B[:name] => antoine
What is a model
Its how a rails application communicates with a data store. The lifeblood of the application
What is views?
Visual representation of the app
What is the recipe to evaluate ruby in html?
- evaluate ruby
- evaluate ruby and print result
How do you create a link?
What is the recipe for a URL generator method?
code is the generator method like method: :delete
What are controllers?
Brains of the application.
Controls our models and views.
Recipe for params
params = { id: “1” }
When are strong parameters required?
When creating or updating with multiple attributes.
What is unless?
The opposite of IF statement. It checks to see whether something is false or not.
What does .include? do?
if string_to_check.include? “substring”
Checks whether the users input contains “something”.
Evaluates to true if it finds what its looking for and false otherwise.
Ruby methods that end with ?
Evaluate to the boolean values true or false
What does .gsub! Stand for?
Global substition.
string_to_change.gsub!(/substring_to_change/, “replacement string”)
Inclusive and exclusive ranges
for num in 1…10- three dots excludes final number in loop.
for num in 1..10- two dots includes the highest number in the range.
What is an iterator?
A ruby method that repeatedly invokes a block of code.
What is the next method?
Skip over certain steps in the loop.
for i in 1..5
next if i % 2 == 0
print i
end
Literal notation hash
hash = { key1 => value1, key2 => value2, key3 => value3, }
Creating a new hash with Hash.new
hash = Hash.new Then to add to the hash simply put: hash[key] = value To access hash values: puts hash[key]
Iterating over multidimensional arrays.
Array = [[1,2,3], [“red”, “blue”]]
puts Array[0][1]
#the array containing numbers is at index zero in things and 2 is at index one in that sub-array.
Array.each do |sub_array| sub_array.each do |item| puts item end end
How do you use string interpolation?
variable = value
puts “hi your number is #{variable}” >
puts “hi your number is “ + variable.to_s
Explain the difference between parameters and arguments
The argument is a piece of code you actually put between the method's parentheses when you call it and the parameter is the name you put between the methods parentheses when you define it. def square(n) <<< parameter puts n ** 2 end square(12) <<< argument