Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Method Structure

A
def exampleMethod(example1, example2)
  return exampleVariable
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To check length of string add

A

variable.length

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

puts

A

text output with newline at end
converts everything to a string
converts nil elements to empty string
returns nil

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

print

A

text output with no newline at end
converts everything to a string
text output of array includes brackets
returns nil

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

p & pp

A

both: text output is raw objects passed to it
both: returns the same object that was passed to it as an argument
both: good for debugging
pp: attempts to print large hashes and arrays in a more readable format

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

if/else/else if format

A
if condition1
   example
elsif condition2
   example
else
   example
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

while loop format

A

while condition
example
end

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

declaring array

adding to array with shovel

A

newArray = []

newArray &laquo_space;variable

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

Enumerable Iteration Methods
How to iterate through an ARRAY, no passing index
Single line method format
Multiline method format

A

arrayExmaple.each
Single line method for a single line of code in block:
arrayExample.each { |elementName| puts elementName }
Multiline for multiple lines of code in block:
arrayExample.each do |elementName|
puts elementName
puts “example”
end

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

Enumerable Iteration Methods

How to iterate through a STRING, no passing index

A

stringExample.each_char
stringExample.each_char |char|
puts char
end

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

Enumerable Iteration Methods

How to iterate through an ARRAY while passing index

A

arrayExmaple.each_with_index

arrayExample.each_with_index do |elementName, index|
puts elementName
puts index
end

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

Enumerable Iteration Methods

How to iterate through a STRING while passing index

A

stringExample.each_char.with_index do |char, index|
puts char
puts index
end

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

Enumeration refers to

A

Enumeration refers to traversing over objects. In Ruby, we call an object enumerable when it describes a set of items and a method to loop over each of item.

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

Range Enumerable
Iterate from start number to end number (inclusive)
Iterate from start number to end number (excluding end)

A

(start. .end).each
ex: (start..end).each { |num| puts num }

(start. ..end).each
ex: (start…end).each { |num| puts num }

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

Enumerable to repeat something a particular number of times

A
numberOfTimes.times { what to do }
ex: 3.times { puts "echo" }
echo
echo
echo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to find first instance of element/char in array/string

A

arr.index(ele) / str.index(char)

17
Q
What is the output of 
str = ""
greeting = "hi"
str += greeting * 3
puts str
A

hihihi