Chapter 3 Flashcards

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

Expression?

A

Combination of numbers, operators and variables that, when understood by the computer, result in an answer of some form.

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

Number Comparison Operators in Ruby?

A

x > y Greater than
x < y Less than
x == y Equal to
x >= y Greater than or equal to
x <= y Less than or equal to
x <=> y Comparison; returns 0 if x and y are equal, 1 if x is higher, and −1 if y is higher
x != y Not equal to

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

Basic loop

A

.times 5.times

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

Iterator

A

Progresses through a list of items one by one. ..or different multiples:

.each

  1. upto(5)
  2. downto(5)
  3. step(50, 5)
  4. upto(5) { |number| puts number }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

String Literal

A

When a string is embedded directly into code, using quotation marks.

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

Interpolation

A

Embedding expressions and even logic into strings

x = 10 y = 20

puts “#{x} + #{y} = #{x + y}”

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

String Methods

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

Regular Expression

A

is a string that describes a pattern for matching elements in other strings.

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

Operator

A

Something that’s used in an expression to manipulate objects such as+(plus), - (minus), * (multiply), and / (divide). You can also use operators to do comparisons, such as with <, >, and &&.

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

Arrays

A

A collection of objects or values with a defined, regular order.

x = [1, 2, 3, 4]

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

Array Index

A

position within the array.

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

Array Literal

A

Square brackets are used to denote an array literal.

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

Array Element Reference

A

To access a particular element, an array (or a variable containing an array) is followed by the index contained within square brackets.

puts x[2]

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

Array <<

A

is the operator for pushing an item onto the end of an array. It’s equivalent to the push method.

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

Array Push

A

equivalent to <<
x.push(“word”)

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

Array Pop

A

is the process of retrieving and removing items from the end of the array.

x = []
x << “Word”
x << “Play”
x << “Fun”
puts x.pop
puts x.pop
puts x.length

Fun
Play
1

17
Q

Hash

A

A collection of objects or values associated with keys. A key can be used to find its respective value inside a hash, but items inside a hash have no specific order. It’s a lookup table, much like the index of a book or a dictionary.

dictionary = { ‘cat’ => ‘feline animal’, ‘dog’ => ‘canine animal’ }

18
Q

Regular Expresion

A

A way to describe patterns in text that can be matched and compared against.

19
Q

Code block

A

A section of code, often used as an argument to an iterator method, that has no discrete name and that is not a method itself, but that can be called and handled by a method that receives it as an argument

20
Q

Range

A

The representation for an entire range of values between a start point and an endpoint.

(‘A’..’Z’).to_a.each { |letter| print letter }

21
Q

Symbol

A

A unique reference defined by a string prefixed with a colon (for example, :blue or :name). Symbols don’t contain values as variables do, but can be used to maintain a consistent reference within code.
person1 = { :name => “Fred”, :age => 20, :gender => :male }

You might want to consider symbols to be literal constants that have no value, but whose name is the most important factor.

22
Q

Substitutions

A

puts “foobar”.sub(‘bar’, ‘foo’)

gsub does multiple substitutions at once, as this example demonstrates: puts “this is a test”.gsub(‘i’, ‘’)

x = “This is a test” puts x.sub(/^../, ‘Hello’)

23
Q

.scan

A

example. What if you want to iterate through a string and have access to each section of it separately? scan is the iterator method you require:

“xyz”.scan(/./) { |letter| puts letter } x y z

24
Q

Basic Special Characters and Symbols Within Regular Expressions

A
25
Q

Regular Expression Character and Sub-Expression Modifiers

A
26
Q

.each

A

[1, “test”, 2, 3, 4].each { |element| puts element.to_s + “X” }

27
Q

.collect

A

Although each iterates through elements of an array, you can also convert an array on the fly using the collect method:

[1, 2, 3, 4].collect { |element| element * 2 } [2, 4, 6, 8]

28
Q

Flow Control

A

age = 10
if age < 18
puts “You’re too young to use this system”
else
puts “You can use this system”
end

age = 10
unless age >= 18
puts “You’re too young to use this system”
puts “So we’re going to exit your program now”
exit
end

29
Q

Ternary Operator

A

makes it possible for an expression to contain a mini if/else statement.

age = 10 type = age < 18 ? “child” : “adult”

30
Q

Elseif

A

fruit = “orange”
if fruit == “orange”
color = “orange”
elsif fruit == “apple”
color = “green”
elsif fruit == “banana”
color = “yellow”
else
color = “unknown”
end

31
Q

Case Block

A

fruit = “orange”
case fruit
when “orange”
color = “orange”
when “apple”
color = “green”
when “banana”
color = “yellow”
else
color = “unknown”
end

32
Q

While

A

x = 1
while x < 100
puts x
x = x * 2
end

33
Q

Until

A

x = 1
until x > 99
puts x
x = x * 2
end