Beginner stuff Flashcards

You may prefer our related Brainscape-certified 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
Q

?a, ?A, ?x (120), 120.chr

A

number, number, 120, x

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

x= 10, y = 20

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

A

10+20=30

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

interpolation

A

process of inserting the expressions into a string literal.

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

puts “its a #{“bad “ * 3} idea

A

its a bad bad bad idea

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

concatenation

A

joining strings together

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

puts x.to_s + “ + “ + y.to_s (x=10, y=10)

A

10 + 10 (string literal)

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

concatenate “test” and “test”

A

“test” + “test” (testtest)

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

capitalize “test”

A

“test”.capitalize

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

down case “TEST”

A

“TEST”.downcase

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

“test”, take off the last index

A

“test”.chop

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

“test” reverse

A

“test”.reverse

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

“Test”.swapcase

A

“tEST”

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

“test”.length

A

4

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

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

A

“foofoo”

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

puts “this is a test”.gsub(‘i’, ‘’)

A

ths s a test

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

x = “This is a test”

puts x.sub(/..$/, ‘Hello’)

A

“This is a teHello”

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

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

A

“Hellois a test”

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

/ foward slash or backwards

A

foward

43
Q

\ foward slash or backwards

A

backwards

44
Q

regular expression

A

starts with /, end with /.

45
Q

^ anchor expression

A

start from the beginning of any lines within the string

46
Q

.. regular expression

A

any two characters

47
Q

“xyz”.scan(/./) { |letter| puts letter {

A

x
y
z

48
Q

“This is a test”.scan(/\w\w/) { |x| puts x }

A
Th
is
is
te
st
49
Q

\w

A

any alphanumeric character or underscore (letter, digits, or underscore)

50
Q

$

A

anchor at the end of a line

51
Q

\d

A

any digit

52
Q

\s

A

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

53
Q

“The car costs $1000 and the cat costs $10”.scan(/\d+/) do |x|
puts x
end

A

d+ matches as many digits in a row

54
Q

\S

A

any non-whitespace (any visible character)

55
Q

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

A

i
i
a
e

56
Q

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

A
h
i
i
a
e
57
Q

character classes

A

allows you to match against a specific set of characters

58
Q

puts “String has vowels” if “This is a test” =~ /[aeiou]/

A

=~ matching operator

59
Q

puts “String contains no digits” unless “This is a test” =~ /[0-9]/

A

puts String unless someString contains a digit

60
Q

puts “String has vowels” if “This is a test”.match(/[aeiou]/)

A

returns

61
Q

x = “This is a test”.match(/(\w+) (\w+)/)
puts x[0]
puts x[1]
puts x[2]

A

This is
This
is

x[0] contains the data matched by the entire regular expression. x[1,2] match each match group.

62
Q

x=[]
x &laquo_space;“word”
x.push(“word”)

A

adding elements to an empty array

63
Q

x.pop

A

pop element from an array

64
Q

x = [“hello”, “world”]

x.join

A

Helloworld

65
Q

x = [“Word”, “Play”, “Fun”]

puts x.join(‘, ‘)

A

word, play, fun

66
Q

puts “This is a test”.scan(/\w/).join(‘,’)

A

T,h,i,s,i,s,a,t,e,s,t

67
Q

puts “Short sentence. Another. No more.”.split(/./).inspect

A

[“Short sentence”, “ Another”, “ No more”]

68
Q

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

A

[“Words”, “with”, “lots”, “of”, “spaces”]

69
Q

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

A
1X
testX
2X
3X
4X
70
Q

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

A

[2, 4, 6, 8]. convert array on the fly

71
Q
a = [1, "test", 2, 3, 4]
i = 0
while (i < a.length)
puts a[i].to_s + "X"
i += 1
end
A
1X
testX
2X
3X
4X
72
Q

x = [1, 2, 3]
y = [“a”, “b”, “c”]
z = x + y
p z

A

[1, 2, 3, “a”, “b”, “c”]

73
Q

x = []

puts “x is empty” if x.empty?

A

x is empty

74
Q

x = [1, 2, 3, 4, 5]
y = [1, 2, 3]
z = x - y
p z

A

[4, 5]

75
Q

x = [1, 2, 3]
p x.include?(“x”)
p x.include?(3)

A

false

true

76
Q

x = [1, 2, 3]
puts x.first
puts x.last

A

1

3

77
Q

x = [1, 2, 3]

puts x.first(2).join(“-“)

A

1-2

78
Q

x = [1, 2, 3]

p x.reverse

A

[3, 2, 1]

79
Q

Hashes

A

defined as key value pairs

80
Q

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

A

keys point to the values of the dictionary declaration

size : 2

81
Q

x = { “a” => 1, “b” => 2 }

x.each { |key, value| puts “#{key} equals #{value}” }

A

a equals 1

b equals 2

82
Q

x = { “a” => 1, “b” => 2, “c” => 3 }

p x.keys

A

[“a”, “b”, “c”]

83
Q

x = { “a” => 1, “b” => 2 }
x.delete(“a”)
p x

A

{“b” => 2}

84
Q

x = { “a” => 100, “b” => 20 }
x.delete_if { |key, value| value < 25 }
p x

A

{“a”=>100}

85
Q

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

A

prints out the text

86
Q

age = 10
type = age < 18 ? “child” : “adult”
puts “You are a “ + type

A

? :

87
Q

ternary operator

A

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

88
Q

age = 10

puts “You are a “ + (age < 18 ? “child” : “adult”)

A

You are a child

89
Q
fruit = "orange"
if fruit == "orange"
color = "orange"
elsif fruit == "apple"
color = "green"
elsif fruit == "banana"
color = "yellow"
else
color = "unknown"
end
A

color = “orange”

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

color = “orange”

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

“orange”

92
Q

i = 1
i = i * 2 until i > 1000
puts i

A

1024

93
Q

x = [1, 2, 3]
x.each do |y|
puts y
end

A

1
2
3

94
Q

Code Block

A

anonymous, nameless method or function. passed to the method, which then runs the code block for each element.

95
Q
def each_vowel(&code_block)
%w{a e i o u}.each { |vowel| code_block.call(vowel) }
end
each_vowel { |vowel| puts vowel }
A
a
e
i
o
u
96
Q

code_block.call(vowel)

A

call method on code_block to execute the code blockfor each vowel passing in the vowel variable as a parameter each time

97
Q
def each_vowel
%w{a e i o u}.each { |vowel| yield vowel }
end
each_vowel { |vowel| puts vowel }
A
a
e
i
o
u
98
Q

yield

A

yield method which automatically detects any passed code block and passes control to it

99
Q

print_parameter_to_screen = lambda { |x| puts x }

print_parameter_to_screen.call(100

A

store code block within variables

prints 100

100
Q

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

A

ABCDEF….XYZ

101
Q

(‘A’..’Z’).include?(‘r’)

A

false

102
Q

symbols

A

dont contain values or objects like variables do. instead, used to maintain a consistent reference within code.

103
Q

Range

A

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

104
Q

Regular expression

A

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

compared against.