Beginner stuff Flashcards

1
Q

x valid name?

A

yes

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

y2 valid name

A

yes

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

_x valid name?

A

yes

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

7x valid name?

A

nope (starts with digit)

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

this_is_a_test valid name?

A

yes

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

this is a test valid name?

A

no(not a single word)

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

this’is@a’test!

A

no (contains invalid characters: ‘@!

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

this-is-a-test

A

invalid (looks like subtraction)

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

unless means the opposite of if (t/f)

A

True

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

x y

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. times do puts “Test” end

5. times do { puts “Test” }

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

loop that counts from 1 up to 5

A

1.upto(5) {…}

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

loop that counts from 10 down to 1

A

10.downto(1) {…}

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

loops that counts every 5th number up to 100

A

0.step(100, 5) {…}

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

1.upto(5) { |number puts number } // what is |number| mean?

A

allows you to get hold of the number being iterated. The number being iterated is being passed down to the variable “number”.

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

10 / 3 = ?

A

3

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

10.0 / 3 = ?

A

3.333…

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

x.to_f

A

converts x to floating point

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

Pi = 3.141592

A

constant that shouldnt me changed, but can

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

x = %q{…}

A

using quotation marks is only viable for a single line, but if you want to span multiple lines, you can use this.

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

x = «YOLO hi YOLO

A

&laquo_space;marks the start of the string literal and is followed by a delimeter of your choice (YOLO) to end the block of code

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

“abc” * 3

A

abcabcabc

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

puts “x” > “y”

A

false (ruby compares the numbers that represent the characters in the string)

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

puts “y” > “x”

A

true (ruby compares the numbers that represent the characters in the string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
?a, ?A, ?x (120), 120.chr
number, number, 120, x
26
x= 10, y = 20 | puts "#{x} + #{y} = #{x+y}"
10+20=30
27
interpolation
process of inserting the expressions into a string literal.
28
puts "its a #{"bad " * 3} idea
its a bad bad bad idea
29
concatenation
joining strings together
30
puts x.to_s + " + " + y.to_s (x=10, y=10)
10 + 10 (string literal)
31
concatenate "test" and "test"
"test" + "test" (testtest)
32
capitalize "test"
"test".capitalize
33
down case "TEST"
"TEST".downcase
34
"test", take off the last index
"test".chop
35
"test" reverse
"test".reverse
36
"Test".swapcase
"tEST"
37
"test".length
4
38
puts "foobar".sub('bar, 'foo')
"foofoo"
39
puts "this is a test".gsub('i', '')
ths s a test
40
x = "This is a test" | puts x.sub(/..$/, 'Hello')
"This is a teHello"
41
"This is a test".sub(/^../, 'Hello')
"Hellois a test"
42
/ foward slash or backwards
foward
43
\ foward slash or backwards
backwards
44
regular expression
starts with /, end with /.
45
^ anchor expression
start from the beginning of any lines within the string
46
.. regular expression
any two characters
47
"xyz".scan(/./) { |letter| puts letter {
x y z
48
"This is a test".scan(/\w\w/) { |x| puts x }
``` Th is is te st ```
49
\w
any alphanumeric character or underscore (letter, digits, or underscore)
50
$
anchor at the end of a line
51
\d
any digit
52
\s
whitespace(spaces, tabs, newlines, and so on)
53
"The car costs $1000 and the cat costs $10".scan(/\d+/) do |x| puts x end
d+ matches as many digits in a row
54
\S
any non-whitespace (any visible character)
55
"This is a test".scan(/[aeiou]/) { |x| puts x }
i i a e
56
"This is a test".scan(/[a-m]/) { |x| puts x }
``` h i i a e ```
57
character classes
allows you to match against a specific set of characters
58
puts "String has vowels" if "This is a test" =~ /[aeiou]/
=~ matching operator
59
puts "String contains no digits" unless "This is a test" =~ /[0-9]/
puts String unless someString contains a digit
60
puts "String has vowels" if "This is a test".match(/[aeiou]/)
returns
61
x = "This is a test".match(/(\w+) (\w+)/) puts x[0] puts x[1] puts x[2]
This is This is x[0] contains the data matched by the entire regular expression. x[1,2] match each match group.
62
x=[] x << "word" x.push("word")
adding elements to an empty array
63
x.pop
pop element from an array
64
x = ["hello", "world"] | x.join
Helloworld
65
x = ["Word", "Play", "Fun"] | puts x.join(', ')
word, play, fun
66
puts "This is a test".scan(/\w/).join(',')
T,h,i,s,i,s,a,t,e,s,t
67
puts "Short sentence. Another. No more.".split(/\./).inspect
["Short sentence", " Another", " No more"]
68
puts "Words with lots of spaces".split(/\s+/).inspect
["Words", "with", "lots", "of", "spaces"]
69
[1, "test", 2, 3, 4].each { |element| puts element.to_s + "X" }
``` 1X testX 2X 3X 4X ```
70
[1, 2, 3, 4].collect { |element| element * 2 }
[2, 4, 6, 8]. convert array on the fly
71
``` a = [1, "test", 2, 3, 4] i = 0 while (i < a.length) puts a[i].to_s + "X" i += 1 end ```
``` 1X testX 2X 3X 4X ```
72
x = [1, 2, 3] y = ["a", "b", "c"] z = x + y p z
[1, 2, 3, "a", "b", "c"]
73
x = [] | puts "x is empty" if x.empty?
x is empty
74
x = [1, 2, 3, 4, 5] y = [1, 2, 3] z = x - y p z
[4, 5]
75
x = [1, 2, 3] p x.include?("x") p x.include?(3)
false | true
76
x = [1, 2, 3] puts x.first puts x.last
1 | 3
77
x = [1, 2, 3] | puts x.first(2).join("-")
1-2
78
x = [1, 2, 3] | p x.reverse
[3, 2, 1]
79
Hashes
defined as key value pairs
80
dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal' }.size
keys point to the values of the dictionary declaration size : 2
81
x = { "a" => 1, "b" => 2 } | x.each { |key, value| puts "#{key} equals #{value}" }
a equals 1 | b equals 2
82
x = { "a" => 1, "b" => 2, "c" => 3 } | p x.keys
["a", "b", "c"]
83
x = { "a" => 1, "b" => 2 } x.delete("a") p x
{"b" => 2}
84
x = { "a" => 100, "b" => 20 } x.delete_if { |key, value| value < 25 } p x
{"a"=>100}
85
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
prints out the text
86
age = 10 type = age < 18 ? "child" : "adult" puts "You are a " + type
? :
87
ternary operator
ternary operator makes it possible for an expression to contain a mini if/else statement.
88
age = 10 | puts "You are a " + (age < 18 ? "child" : "adult")
You are a child
89
``` fruit = "orange" if fruit == "orange" color = "orange" elsif fruit == "apple" color = "green" elsif fruit == "banana" color = "yellow" else color = "unknown" end ```
color = "orange"
90
``` fruit = "orange" case fruit when "orange" color = "orange" when "apple" color = "green" when "banana" color = "yellow" else color = "unknown" end ```
color = "orange"
91
``` fruit = "orange" color = case fruit when "orange" "orange" when "apple" "green" when "banana" "yellow" else "unknown" end ```
"orange"
92
i = 1 i = i * 2 until i > 1000 puts i
1024
93
x = [1, 2, 3] x.each do |y| puts y end
1 2 3
94
Code Block
anonymous, nameless method or function. passed to the method, which then runs the code block for each element.
95
``` def each_vowel(&code_block) %w{a e i o u}.each { |vowel| code_block.call(vowel) } end each_vowel { |vowel| puts vowel } ```
``` a e i o u ```
96
code_block.call(vowel)
call method on code_block to execute the code blockfor each vowel passing in the vowel variable as a parameter each time
97
``` def each_vowel %w{a e i o u}.each { |vowel| yield vowel } end each_vowel { |vowel| puts vowel } ```
``` a e i o u ```
98
yield
yield method which automatically detects any passed code block and passes control to it
99
print_parameter_to_screen = lambda { |x| puts x } | print_parameter_to_screen.call(100
store code block within variables | prints 100
100
('A'..'Z').to_a.each { |letter| print letter }
ABCDEF....XYZ
101
('A'..'Z').include?('r')
false
102
symbols
dont contain values or objects like variables do. instead, used to maintain a consistent reference within code.
103
Range
The representation for an entire range of values between a start point and an endpoint.
104
Regular expression
A way to describe patterns in text that can be matched and | compared against.