Ruby Flashcards

0
Q

See how many letters in a string

A

“String”.length = 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Flip letters backwards

A

“String”.reverse = gnirtS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Multiplying strings

A

“String” * 5 = stringstringstringstringstring

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convert an things into a string

A

40.to_s = 40

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Convert things into integers

A
'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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are [ ]?

A

Empty lists. Lists store things in order.

Like a line for popcorn.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Check which one is the highest in an array.

A

[12, 47, 35].max = 47

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a variable?

A

A place where you store programs and save them for later use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Arranging values in order

A

Ticket.sort

[12,35,47]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are arrays?

A

Lists for storing things in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Converts things into floating point number.

A

99.to_f = 99.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Convert things into an array.

A

.to_a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Convert things into list.

A

.lines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Target and find things then search and replace.

A

poem[‘string’]= ‘replacement’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is chaining?

A
Combining methods to get a lot more done. 
#ex. poem.lines.to_a.reverse.join - break up a poem, reverse it, reassemble it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is { }?

A

An empty hash. A little dictionary. Hash is a series of key value.
Hash recipe: variable = {key:value}
Example:
B= {name : antoine}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are symbols?

A
\: tiny efficient code words with a colon. 
#ex. :splendid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are blocks?

A
Chunks of code which can be tacked on to many of rubys methods.
#ex. 
books.values.each { |rate| ratings[rate] += 1}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is def

A
def means define. Define a method or create one. 
#ex. 
def look <<< method name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is File.foreach?

A

A method which opens a file and hands each line to the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What’s split?

A

A method for strings, which breaks the string up into an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is strip?

A

A method that removes extra spaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is attr?

A

Attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are classes?

A

Everything in Ruby is some kind of object. Classes explain objects. How a certain object works.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are accessors?

A

Variable attached to an object which can be used outside the object.

25
Q

What are instance variables?

A

Grants view access to variables with @

26
Q

What are the string formatting methods?

A
ljust, center, rjust. 
#ex. 
Line_width = 40 
str = '--> text 
Puts(str.ljust( line_width))
Puts(str.center( line_width))
Puts(str.rjust( line_width))
27
Q

What are the arithmetic methods?

A
\+ Addition
- subtraction
* multiplication
/ division 
** exponention or square of a number
% modulus or remainder
28
Q

What is abs?

A

This method simply returns the absolute value of the number

#ex. 
puts (5+2).abs = 3
puts (2+5).abs = 3
29
Q

How does the rand method work?

A
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
30
Q

What is srand?

A

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

31
Q

What is the Math object?

A
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
32
Q

What are the comparison methods?

A

> greater than
< less than
== equal to
!= not equal to

33
Q

What are the logical operators?

A

&& 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

34
Q

What is branching?

A

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?

35
Q

What is looping?

A

Makes the computer do the same thing over and over again.

36
Q

What is a while loop?

A

A while loop works kinda like IF. while something is true then keep looping or ELSE do something else or END.

37
Q

What is recursion?

A

A method that calls itself just like a loop but doesnt loop forever

38
Q

What are factorials?

A
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
39
Q

What is the recipe for reading a value out of a hash?

A

Variable[:key] => value
Example
B[:name] => antoine

40
Q

What is a model

A

Its how a rails application communicates with a data store. The lifeblood of the application

41
Q

What is views?

A

Visual representation of the app

42
Q

What is the recipe to evaluate ruby in html?

A
  • evaluate ruby

- evaluate ruby and print result

43
Q

How do you create a link?

A
44
Q

What is the recipe for a URL generator method?

A

code is the generator method like method: :delete

45
Q

What are controllers?

A

Brains of the application.

Controls our models and views.

46
Q

Recipe for params

A

params = { id: “1” }

47
Q

When are strong parameters required?

A

When creating or updating with multiple attributes.

48
Q

What is unless?

A

The opposite of IF statement. It checks to see whether something is false or not.

49
Q

What does .include? do?

A

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.

50
Q

Ruby methods that end with ?

A

Evaluate to the boolean values true or false

51
Q

What does .gsub! Stand for?

A

Global substition.

string_to_change.gsub!(/substring_to_change/, “replacement string”)

52
Q

Inclusive and exclusive ranges

A

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.

53
Q

What is an iterator?

A

A ruby method that repeatedly invokes a block of code.

54
Q

What is the next method?

A

Skip over certain steps in the loop.

for i in 1..5
next if i % 2 == 0
print i
end

55
Q

Literal notation hash

A
hash = {
 key1 => value1, 
 key2 => value2,
 key3 => value3,
}
56
Q

Creating a new hash with Hash.new

A
hash = Hash.new
Then to add to the hash simply put:
hash[key] = value
To access hash values:
puts hash[key]
57
Q

Iterating over multidimensional arrays.

A

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
58
Q

How do you use string interpolation?

A

variable = value
puts “hi your number is #{variable}” >
puts “hi your number is “ + variable.to_s

59
Q

Explain the difference between parameters and arguments

A
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