Begging Ruby Chapter 1 Flashcards

1
Q

What is object orientation

A

everything is an object guitar amps studio

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

Data Types

A

variable can hold these

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

Control Structures

A

variable can hold these

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

Is Ruby Open Source?

A

Yes

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

Ruby Interpreter

A

This is a ruby program that understands other programs written in the Ruby language, along with a collection of extensions and libraries to make your Ruby more fully featured

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

irb: Interactive Ruby

A

irb stands for “Interactive Ruby.” “Interactive” means that as soon as you type something, your computer will immediately attempt to process it. Sometimes this sort of environment is called an immediate or interactive environment.

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

object-oriented programming language

A

In the simplest sense, this means that your Ruby programs can define and operate upon concepts in a real-world fashion. Your program can contain concepts such as “people,” “boxes,” “tickets,” “maps,” or any other concept you want to work with. Object-oriented languages make it easy to implement these concepts in
a way that you can create objects based upon them. Object oriented languages can then act upon and understand the relationships between these concepts in any way you define.

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

nouns / verbs

A

Objects are nouns, methods are verbs.

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

3 corners of OOP

A

Encapsulation, Inheritance, Polymorphism

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

OOP

A

Style of programming where you create representations of “types” of objects like guitars and amps (Classes), and then can create specific objects like 69 strat, black les paul, 80s peavey, (instance of an object) that intend to use with each other.

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

attr_accessor

A

attr stands for “attribute,” and accessor roughly means “make these attributes accessible to be set and changed at will.”

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

print vs puts

A

puts automatically moves the output cursor to the next line

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

what can a ruby variable contain

A

numbers, text, and other data structures

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

class

A

A class is the definition of a single type of object

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

standard attribute syntax

A

attr_accessor :name, :age, :gender

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

what does inheritance allow you to do

A

Inheritance allows different classes to relate to one another and group concepts by their similarities

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

classless method

A

def guitar_strum
puts “brrriiing”
end

guitar_strum

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

Class

A

Class: A class is a definition of a concept in an object-oriented language such as Ruby. We created classes called Pet, Dog, Cat, Snake, and Person. Classes can inherit features from other classes, but still have unique features of their own.

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

Method

A

Method: A method represents a set of code (containing multiple commands and statements) within a class and/or an object. For example, our Dog class objects had a bark method that printed “Woof!” to the screen. Methods can also be directly linked to classes, as with fred = Person.new, where new is a method that creates a new object based upon the Person class. Methods can also accept data—known as arguments or parameters—included in parentheses after the method name, as with puts(“Test”).

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

Arguments/parameters

A

Arguments/parameters: These are the data passed to methods in parentheses (or, as
in some cases, following the method name without parentheses, as in puts “Test”). Technically, you pass arguments to methods, and methods receive parameters, but for pragmatic purposes, the terms are interchangeable.

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

Kernel

A
Some methods don’t require a class name to be usable, such as puts. These
are usually built-in, common methods that don’t have an obvious connection to any classes. Many of these methods are included in Ruby’s Kernel module, a module that provides functions that work from anywhere within Ruby code without being explicitly referred to.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Experimentation

A

One of the most fulfilling things about programming is that you can turn your dreams into reality. The amount of skill you need varies with your dreams, but generally if you want to develop a certain type of application or service, you can give it a try. Most software comes from necessity or a dream, so keeping your eyes and ears open for things you might want to develop is important. It’s even more important when you first get practical knowledge of a new language, as you are while reading this book. If an idea crosses your mind, break it down into the smallest components that you can represent as Ruby classes and see if you can put together the building blocks with the Ruby you’ve learned so far. Your programming skills can only improve with practice.

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

simple do if syntax

A

age = 10

puts “You’re too young to use this system” if age

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

string literal

A

When a string is embedded directly into code, using quotation marks as earlier, the con- struction is called a string literal. This differs from a string whose data comes from a remote source, such as a user typing in text, a file, or the Internet. Any text that’s pre-embedded within a program is a string literal.

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

interpolation

A

interpolation refers to the process of inserting the result of an expression into a string literal “#{x+y}”

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

“Test” + “Test”

A

TestTest

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

“Test”.capitalize

A

Test

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

“Test”.downcase

A

test

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

“Test”.chop

A

Tes

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

“Test”.next

A

Tesu

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

“Test”.reverse

A

tseT

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

“Test”.sum

A

416

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

“Test”.swapcase

A

tEST

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

“Test”.upcase

A

TEST

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

“Test”.upcase.reverse

A

TSET

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

“Test”.upcase.reverse.next

A

TSEU

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

regular expression (string)

A

A regular expression, therefore, 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
38
Q

basic substitution

A

puts “yo la tango”.sub(‘tango’, ‘mango’)

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

regular expression

A

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

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

regular expression substitution of first two characters syntax

A

x.sub(/^../, ‘Hello’)

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

iterate through a string and have access to each section of it separately

A

“xyz”.scan (/./) { |character| puts character }

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

any alphanumeric character or an underscore

A

\w

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

regular expression argument general syntax

A

(/something/)

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

Anchor for the beginning of a line

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

Anchor for the end of a line

A

$

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

Anchor for the start of a string

A

\A

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

Anchor for the end of a string

A

\Z

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

Any character

A

.

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

Any letter, digit, or underscore

A

\w

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

Anything that \w doesn’t match

A

\W

51
Q

Any digit

A

\d

52
Q

Anything that \d doesn’t match (non-digits)

A

\D

53
Q

Whitespace (spaces, tabs, newlines, and so on)

A

\s

54
Q

Non-whitespace (any visible character)

A

\S

55
Q

scanning for each character but adding them together to make a word

A

.scan (/.+/)

56
Q

scan for vowels syntax

A

“This is a test”.scan(/[aeiou]/) { |x| puts x }

57
Q

matching operator

A

=~

58
Q

array to string syntax

A

x.join(‘ ‘)

59
Q

turning a string into an array syntax

A

“Words with lots of spaces”.split(/\s+/)

60
Q

basic hash iteration syntax

A

x.each { |key, value| something happens here with each }

61
Q

ternary operator

A

2 > 1 ? “yes” : “no”

62
Q

ternary syntax

A

2 > 1 ? “yes” : “no”

63
Q

x y

A

Comparison; returns 0 if x and y are equal, 1 if x is higher, and -1 if y is higher

64
Q

turn beginning two elements of array into string with dash

A

array.first(2).join(“-“)

65
Q

discarding hash elements

A

x.delete(key)

66
Q

discard hash conditional example

A

hash_name.delete_if { |key, value| value

67
Q

fruit case example

A
fruit = "orange"
color = case fruit
 when "orange"
 "orange"
 when "apple"
 "green"
 when "banana"
 "yellow"
 else
 "unknown"
end
68
Q

code block

A

essentially an anonymous, nameless method or function

[{} or do end]

69
Q

how to turn letters a through z into an array

A

(‘A’..’Z’).to_a

70
Q

hash basic syntax

A

s = { :key => ‘value’ }

71
Q

Variable

A

A placeholder that can hold an object—from numbers, to text, to arrays, to objects of your own creation.

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

73
Q

Integer

A

A whole number, such as 5 or 923737.

74
Q

Float

A

A number with a decimal portion, such as 1.0 or 3.141592.

75
Q

Character

A

A single letter, digit, unit of space, or typographic symbol.

76
Q

String

A

A collection of characters such as Hello, world! or Ruby is cool.

77
Q

Constant

A

A variable with a fixed value. Constant variable names begin with a capital letter.

78
Q

Iterator

A

A special method such as each, upto, or times that steps through a list element by element. This process is called iteration, and each, upto, and times are iterator methods.

79
Q

Interpolation

A

The mixing of expressions into strings.

80
Q

Array

A

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

81
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.

82
Q

Regular expression

A

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

83
Q

Flow control

A

The process of managing which sections of code to execute based on certain conditions and states.

84
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. Code blocks can also be stored in variables as objects of the Proc class (or as lambdas).

85
Q

Range

A

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

86
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. They can be considered as identifiers or constants that stand alone in what they abstractly represent.

87
Q

. .

A

including

88
Q

. . .

A

not including

89
Q

how would you check if letters a through z has r in it

A

(‘a’..’z’).include?(‘r’)

90
Q

create a new triangle

A

Triangle.new(1,2,3)

91
Q

what is the benefit of inheritence?

A

classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own

92
Q

what is the escape charecter

A

backslash \

93
Q

print first 3 letters of “string”

A

p “string”[0..2]

94
Q

how to create a lambda

A

l = lambda { “Do or do not” }

puts l.call

95
Q

increase of all menu items by 10%

A

menu_items = {:curry => 4, :roti => 6}
menu_items.each do |item, price|
menu_times[item] = price * 1.1
end

96
Q

.count

A

.count(argument) goes through an array and counts how many time argument appears

97
Q

Select random elements from an array

A

def random_select(array, n)
result = []
n.times do
result

98
Q

newest hash syntax

A

{ k: v }

99
Q

how to spell initialize

A

Init i alize

100
Q

summing elements in an array

A

inject(0) { |sum, num| sum + num }

101
Q

syntax for simple do if w/ two conditionals

A

do if condition1 == true && condition2 == true

102
Q

do using unless

A

do unless conditional1==true

103
Q

not equal to comparison operator

A

!=

104
Q

how to loop from n to m

A

n.upto(m) { code to loop }

105
Q

how to code loop from m down to n

A

m.downto(n) { code to loop }

106
Q

0 to 50 in 5 steps

A

0.step(50, 5) { code to loop }

107
Q

using string delimiters

A

x =

108
Q

result of “abc” * 5

A

abcabcabcabcabc

109
Q

basic interpolation

A

”#{something}”

110
Q

define regular expression

A

a search query

111
Q

how to iterate through a string

A

‘string’.scan(/./){ |char| do something }

112
Q

returning a string with no spaces

A

“this is a crazy sentence 123”.scan(/\S+/).join

113
Q

matching operator

A

=~

114
Q

.scan vs .match

A

.scan goes though everything, . match returns one match value

115
Q

method to check if array has no elements

A

array.empty?

116
Q

does an array have an element

A

array.include?(ImptArrgument)

117
Q

methods for array and hash

A
.delete
.delete_if
.first
.collect
.each
118
Q

methods for array

A
.first
.first(arg)
.last
.last(arg)
.reverse
119
Q

hash methods

A
.size
.each
.keys
.values
.delete
.delete_if
.first
120
Q

how to create a new hash which saves changed old hash

A

new_hash = {}; old_hash.each { |key,val| new_hash[key] = val + something }

121
Q

demonstration of a class method

A
class Thing
  def self.thing_method
     puts "hello from class method"
  end
122
Q

%w{ this is a long sentence }

A

[ “this”, “is”, “a”, “long”, “sentence” ]

123
Q

%q{ this is a long sentence }

A

” this is a long sentence “

124
Q

.collect

A

Returns a new array with the results of running block once for every element in enum.