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.