Real ruby Flashcards

1
Q

alias

A

To create a second name for the variable or method.

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

and

A

A command that appends two or more objects together.

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

BEGIN

A

Designates code that must be run unconditionally at the beginning of the program before any other.

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

begin

A

Delimits a “begin” block of code, which can allow the use of while and until in modifier position with multi-line statements.

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

break

A

Gives an unconditional termination to a code block, and is usually placed with an argument.

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

case

A

starts a case statement; this block of code will output a result and end when it’s terms are fulfilled, which are defined with when or else.

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

class

A

Opens a class definition block, which can later be reopened and added to with variables and even functions.

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

def

A

Used to define a function.

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

defined?

A

A boolean logic function that asks whether or not a targeted expression refers to anything recognizable in Ruby; i.e. literal object, local variable that has been initialized, method name visible from the current scope, etc.

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

do

A

Paired with end, this can delimit a code block, much like curly braces; however, curly braces retain higher precedence.

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

else

A

Gives an “otherwise” within a function, if-statement, or for-loop, i.e. if cats = cute, puts “Yay!” else puts “Oh, a cat.”

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

elsif

A

Much like else, but has a higher precedence, and is usually paired with terms.

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

END

A

Designates, via code block, code to be executed just prior to program termination.

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

end

A

Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct.

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

ensure

A

Marks the final, optional clause of a begin/end block, generally in cases where the block also contains a rescue clause. The code in this term’s clause is guaranteed to be executed, whether control flows to a rescue block or not.

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

FALSE

A

denotes a special object, the sole instance of FalseClass. false and nil are the only objects that evaluate to Boolean falsehood in Ruby (informally, that cause an if condition to fail.)

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

for

A

A loop constructor; used in for-loops.

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

if

A

Ruby’s basic conditional statement constructor.

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

in

A

Used with for, helps define a for-loop.

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

module

A

Opens a library, or module, within a Ruby Stream.

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

next

A

Bumps an iterator, or a while or until block, to the next iteration, unconditionally and without executing whatever may remain of the block.

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

nil

A

A special “non-object”; it is, in fact, an object (the sole instance of NilClass), but connotes absence and indeterminacy. nil and false are the only two objects in Ruby that have Boolean falsehood (informally, that cause an if condition to fail).

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

not

A

Boolean negation. i.e. not true # false, not 10 # false, not false # true.

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

or

A

Boolean or. Differs from || in that or has lower precedence.

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

redo

A

Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.

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

rescue

A

Designates an exception-handling clause that can occur either inside a begin/end block, inside a method definition (which implies begin), or in modifier position (at the end of a statement).

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

retry

A

Inside a rescue clause, causes Ruby to return to the top of the enclosing code (the begin keyword, or top of method or block) and try executing the code again.

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

return

A

Inside a method definition, executes the ensure clause, if present, and then returns control to the context of the method call. Takes an optional argument (defaulting to nil), which serves as the return value of the method. Multiple values in argument position will be returned in an array.

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

self

A

The “current object” and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context.

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

super

A

Called from a method, searches along the method lookup path (the classes and modules available to the current object) for the next method of the same name as the one being executed. Such method, if present, may be defined in the superclass of the object’s class, but may also be defined in the superclass’s superclass or any class on the upward path, as well as any module mixed in to any of those classes.

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

then

A

Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.

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

TRUE

A

The sole instance of the special class TrueClass. true encapsulates Boolean truth; however, all objects in Ruby are true in the Boolean sense (informally, they cause an if test to succeed), with the exceptions of false and nil.

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

undef

A

Undefines a given method, for the class or module in which it’s called. If the method is defined higher up in the lookup path (such as by a superclass), it can still be called by instances classes higher up.

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

unless

A

The negative equivalent of if. i.e. unless y.score > 10 puts “Sorry; you needed 10 points to win.” end.

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

until

A

The inverse of while: executes code until a given condition is true, i.e., while it is not true. The semantics are the same as those of while.

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

when

A

Same as case.

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

while

A

Takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.

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

yield

A

Called from inside a method body, yields control to the code block (if any) supplied as part of the method call. If no code block has been supplied, calling yield raises an exception.

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

check if an integer is even

A

integer.even?

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

check if an integer is odd

A

integer.odd?

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

find the next integer

A

integer.next

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

is an integer between two to others?

A

integer.between? 1,3

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

reverse a string

A

string.reverse

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

find out how long a string is

A

string.length

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

make a string all capitals

A

string.upcase

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

make a string all small letters

A

string.downcase

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

reverse the cases of a string

A

string.swapcase

48
Q

capitalize the first letter of a string

A

string.capitalize

49
Q

justify a string to the middle

A

string.center

50
Q

pad the right side of a string with repeating zeroes

A

string.ljust(5,’0’)

51
Q

pad the left side of a string with repeating zeroes

A

string.rjust(5,’0’

52
Q

find out if a string starts with a substring

A

string.start_with? ‘Substring’

53
Q

find out if a string ends with a substring

A

string.end_with? ‘susbstring

54
Q

find the index of a particular letter in a string

A

string.index ‘A’

55
Q

split a string into an array

A

string.split(‘’)

56
Q

find and replace the first instance of a string in another string

A

string.sub(‘I’,’We)

57
Q

find and replace all instances of a string in another string

A

string.gsub(‘I’,’We’)

58
Q

return a substring

A

newString = string[2..6]

59
Q

extract all the a-z words from a sentence

A

senA = sen.downcase.gsub(.[^a-z\s]/,’’).split(‘ ‘)

60
Q

generate a random decimal between 0 and 1

A

rand

61
Q

generate a number between 1 and 10

A

rand(11)

62
Q

pi

A

Math::PI

63
Q

e (as in logarithm)

A

Math::E

64
Q

cosine

A

Math.cos

65
Q

tangent

A

Math.tan

66
Q

square root

A

Math.sqrt(x)

67
Q

retrieve an item from an array using an index

A

array[index]

68
Q

retrieve an item from the end of an array using index

A

array[-index]

69
Q

retrieve an items index from an array

A

array.index(‘thing’)

70
Q

print an array as a string with a divider

A

array.join(‘ ‘)

71
Q

add item to the end of an array

A

array.push ‘item’ OR array &laquo_space;‘item’

72
Q

remove item from the end of an array

A

array.pop

73
Q

get the length of an array

A

array.length

74
Q

return the unique elements of an array

A

array.uniq

75
Q

do an action on every element of an array, altering array

A

array.map{|i| i*3}

76
Q

filter the elements of an array into a new array

A

array.select{|thing| thing.length > 5}

77
Q

delete an element of an array

A

array.delete(‘item’)

78
Q

delete an element of an array on a condition

A

array.delete(|thing| thing%2==0)

79
Q

create a new time object

A

time = Time.new

80
Q

add one minute to a time object

A

time2 = time + 60

81
Q

create new hash

A

newHash = {} or newHash = Hash.new

82
Q

pad the right side of a string with blank space

A

string.ljust(20)

83
Q

pad the left side of a string with blank space

A

string.rjust(20)

84
Q

determine whether a hash has a certain key

A

hash.include?(‘key’)

85
Q

print ascii codes for a string

A

str.each_byte{|c| put c}

86
Q

print ascii character from code

A

int.chr

87
Q

get ascii code for a single char

A

char.ord

88
Q

remove all nils from an array

A

array.compact

89
Q

return the maximum value from a hash

A

hash.max_by{|k,v| v}

90
Q

return permutations of an array as an array

A

array.permutation.to_a

91
Q

go through a loop, iterating by a number other than 1

A

0.step(0,50) {loop the code in here…}

92
Q

declare a variable as a constant

A

Pi = 3.1415

93
Q

return ASCII code for a character (not .ord)

A

?a, ?Z

94
Q

regular expression meaning any two characters immediately after the beginning of a line

A

/^../

95
Q

regular expression meaning the last two characters of a line

A

/..$/

96
Q

regex first two characters of absolute start of a string

A

/\A../

97
Q

regex last two characters of absolute end of a string

A

/..\Z/

98
Q

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

A

This is a test.scan(/../) {|x| puts x}

99
Q

regex anchor for beginning of line

A
100
Q

regex anchor for end of line

A

$

101
Q

regex anchor for beginning of a string

A

\A

102
Q

regex anchor for the end of a string

A

\Z

103
Q

regex any character

A

.

104
Q

regex any letter, digit, or underscore

A

\w

105
Q

regex any character that \w doesn’t match

A

\W

106
Q

regex any digit

A

\d

107
Q

regex any non-digit

A

\D

108
Q

regex whitespace

A

\s

109
Q

regex non-whitespace

A

\S

110
Q

regex match one or more of a certain type of character

A

+, e.g. string.scan(/\d+/) do |x| gives all numbers in a string

111
Q

return first number of elements of an array, e.g. first 3 element

A

array.first(3)

112
Q

return an array of all the keys of a hash

A

hash.keys

113
Q

the ternary operator

A

?

114
Q

.collect

A

Array#collect, iterates over the array with code block and returns new array

115
Q

.strptime

A

DateTime#strptime