Begging Ruby Chapter 1 Flashcards

(124 cards)

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
interpolation
interpolation refers to the process of inserting the result of an expression into a string literal "#{x+y}"
26
"Test" + "Test"
TestTest
27
"Test".capitalize
Test
28
"Test".downcase
test
29
"Test".chop
Tes
30
"Test".next
Tesu
31
"Test".reverse
tseT
32
"Test".sum
416
33
"Test".swapcase
tEST
34
"Test".upcase
TEST
35
"Test".upcase.reverse
TSET
36
"Test".upcase.reverse.next
TSEU
37
regular expression (string)
A regular expression, therefore, is a string that describes a pattern for matching elements in other strings.
38
basic substitution
puts "yo la tango".sub('tango', 'mango')
39
regular expression
puts "foobar".sub('bar', 'foo')
40
regular expression substitution of first two characters syntax
x.sub(/^../, 'Hello')
41
iterate through a string and have access to each section of it separately
"xyz".scan (/./) { |character| puts character }
42
any alphanumeric character or an underscore
\w
43
regular expression argument general syntax
(/something/)
44
Anchor for the beginning of a line
^
45
Anchor for the end of a line
$
46
Anchor for the start of a string
\A
47
Anchor for the end of a string
\Z
48
Any character
.
49
Any letter, digit, or underscore
\w
50
Anything that \w doesn’t match
\W
51
Any digit
\d
52
Anything that \d doesn’t match (non-digits)
\D
53
Whitespace (spaces, tabs, newlines, and so on)
\s
54
Non-whitespace (any visible character)
\S
55
scanning for each character but adding them together to make a word
.scan (/.+/)
56
scan for vowels syntax
"This is a test".scan(/[aeiou]/) { |x| puts x }
57
matching operator
=~
58
array to string syntax
x.join(' ')
59
turning a string into an array syntax
"Words with lots of spaces".split(/\s+/)
60
basic hash iteration syntax
x.each { |key, value| something happens here with each }
61
ternary operator
2 > 1 ? "yes" : "no"
62
ternary syntax
2 > 1 ? "yes" : "no"
63
x y
Comparison; returns 0 if x and y are equal, 1 if x is higher, and -1 if y is higher
64
turn beginning two elements of array into string with dash
array.first(2).join("-")
65
discarding hash elements
x.delete(key)
66
discard hash conditional example
hash_name.delete_if { |key, value| value
67
fruit case example
``` fruit = "orange" color = case fruit when "orange" "orange" when "apple" "green" when "banana" "yellow" else "unknown" end ```
68
code block
essentially an anonymous, nameless method or function | [{} or do end]
69
how to turn letters a through z into an array
('A'..'Z').to_a
70
hash basic syntax
s = { :key => 'value' }
71
Variable
A placeholder that can hold an object—from numbers, to text, to arrays, to objects of your own creation.
72
Operator
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
Integer
A whole number, such as 5 or 923737.
74
Float
A number with a decimal portion, such as 1.0 or 3.141592.
75
Character
A single letter, digit, unit of space, or typographic symbol.
76
String
A collection of characters such as Hello, world! or Ruby is cool.
77
Constant
A variable with a fixed value. Constant variable names begin with a capital letter.
78
Iterator
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
Interpolation
The mixing of expressions into strings.
80
Array
A collection of objects or values with a defined, regular order.
81
• Hash
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
Regular expression
A way to describe patterns in text that can be matched and compared against.
83
Flow control
The process of managing which sections of code to execute based on certain conditions and states.
84
Code block
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
Range
The representation for an entire range of values between a start point and an endpoint.
86
Symbol
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
. .
including
88
. . .
not including
89
how would you check if letters a through z has r in it
('a'..'z').include?('r')
90
create a new triangle
Triangle.new(1,2,3)
91
what is the benefit of inheritence?
classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own
92
what is the escape charecter
backslash \
93
print first 3 letters of "string"
p "string"[0..2]
94
how to create a lambda
l = lambda { "Do or do not" } | puts l.call
95
increase of all menu items by 10%
menu_items = {:curry => 4, :roti => 6} menu_items.each do |item, price| menu_times[item] = price * 1.1 end
96
.count
.count(argument) goes through an array and counts how many time argument appears
97
Select random elements from an array
def random_select(array, n) result = [] n.times do result
98
newest hash syntax
{ k: v }
99
how to spell initialize
Init i alize
100
summing elements in an array
inject(0) { |sum, num| sum + num }
101
syntax for simple do if w/ two conditionals
do if condition1 == true && condition2 == true
102
do using unless
do unless conditional1==true
103
not equal to comparison operator
!=
104
how to loop from n to m
n.upto(m) { code to loop }
105
how to code loop from m down to n
m.downto(n) { code to loop }
106
0 to 50 in 5 steps
0.step(50, 5) { code to loop }
107
using string delimiters
x =
108
result of "abc" * 5
abcabcabcabcabc
109
basic interpolation
"#{something}"
110
define regular expression
a search query
111
how to iterate through a string
'string'.scan(/./){ |char| do something }
112
returning a string with no spaces
"this is a crazy sentence 123".scan(/\S+/).join
113
matching operator
=~
114
.scan vs .match
.scan goes though everything, . match returns one match value
115
method to check if array has no elements
array.empty?
116
does an array have an element
array.include?(ImptArrgument)
117
methods for array and hash
``` .delete .delete_if .first .collect .each ```
118
methods for array
``` .first .first(arg) .last .last(arg) .reverse ```
119
hash methods
``` .size .each .keys .values .delete .delete_if .first ```
120
how to create a new hash which saves changed old hash
new_hash = {}; old_hash.each { |key,val| new_hash[key] = val + something }
121
demonstration of a class method
``` class Thing def self.thing_method puts "hello from class method" end ```
122
%w{ this is a long sentence }
[ "this", "is", "a", "long", "sentence" ]
123
%q{ this is a long sentence }
" this is a long sentence "
124
.collect
Returns a new array with the results of running block once for every element in enum.