Real ruby Flashcards
alias
To create a second name for the variable or method.
and
A command that appends two or more objects together.
BEGIN
Designates code that must be run unconditionally at the beginning of the program before any other.
begin
Delimits a “begin” block of code, which can allow the use of while and until in modifier position with multi-line statements.
break
Gives an unconditional termination to a code block, and is usually placed with an argument.
case
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.
class
Opens a class definition block, which can later be reopened and added to with variables and even functions.
def
Used to define a function.
defined?
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.
do
Paired with end, this can delimit a code block, much like curly braces; however, curly braces retain higher precedence.
else
Gives an “otherwise” within a function, if-statement, or for-loop, i.e. if cats = cute, puts “Yay!” else puts “Oh, a cat.”
elsif
Much like else, but has a higher precedence, and is usually paired with terms.
END
Designates, via code block, code to be executed just prior to program termination.
end
Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct.
ensure
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.
FALSE
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.)
for
A loop constructor; used in for-loops.
if
Ruby’s basic conditional statement constructor.
in
Used with for, helps define a for-loop.
module
Opens a library, or module, within a Ruby Stream.
next
Bumps an iterator, or a while or until block, to the next iteration, unconditionally and without executing whatever may remain of the block.
nil
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).
not
Boolean negation. i.e. not true # false, not 10 # false, not false # true.
or
Boolean or. Differs from || in that or has lower precedence.
redo
Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.
rescue
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).
retry
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.
return
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.
self
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.
super
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.
then
Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.
TRUE
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.
undef
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.
unless
The negative equivalent of if. i.e. unless y.score > 10 puts “Sorry; you needed 10 points to win.” end.
until
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.
when
Same as case.
while
Takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.
yield
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.
check if an integer is even
integer.even?
check if an integer is odd
integer.odd?
find the next integer
integer.next
is an integer between two to others?
integer.between? 1,3
reverse a string
string.reverse
find out how long a string is
string.length
make a string all capitals
string.upcase
make a string all small letters
string.downcase
reverse the cases of a string
string.swapcase
capitalize the first letter of a string
string.capitalize
justify a string to the middle
string.center
pad the right side of a string with repeating zeroes
string.ljust(5,’0’)
pad the left side of a string with repeating zeroes
string.rjust(5,’0’
find out if a string starts with a substring
string.start_with? ‘Substring’
find out if a string ends with a substring
string.end_with? ‘susbstring
find the index of a particular letter in a string
string.index ‘A’
split a string into an array
string.split(‘’)
find and replace the first instance of a string in another string
string.sub(‘I’,’We)
find and replace all instances of a string in another string
string.gsub(‘I’,’We’)
return a substring
newString = string[2..6]
extract all the a-z words from a sentence
senA = sen.downcase.gsub(.[^a-z\s]/,’’).split(‘ ‘)
generate a random decimal between 0 and 1
rand
generate a number between 1 and 10
rand(11)
pi
Math::PI
e (as in logarithm)
Math::E
cosine
Math.cos
tangent
Math.tan
square root
Math.sqrt(x)
retrieve an item from an array using an index
array[index]
retrieve an item from the end of an array using index
array[-index]
retrieve an items index from an array
array.index(‘thing’)
print an array as a string with a divider
array.join(‘ ‘)
add item to the end of an array
array.push ‘item’ OR array «_space;‘item’
remove item from the end of an array
array.pop
get the length of an array
array.length
return the unique elements of an array
array.uniq
do an action on every element of an array, altering array
array.map{|i| i*3}
filter the elements of an array into a new array
array.select{|thing| thing.length > 5}
delete an element of an array
array.delete(‘item’)
delete an element of an array on a condition
array.delete(|thing| thing%2==0)
create a new time object
time = Time.new
add one minute to a time object
time2 = time + 60
create new hash
newHash = {} or newHash = Hash.new
pad the right side of a string with blank space
string.ljust(20)
pad the left side of a string with blank space
string.rjust(20)
determine whether a hash has a certain key
hash.include?(‘key’)
print ascii codes for a string
str.each_byte{|c| put c}
print ascii character from code
int.chr
get ascii code for a single char
char.ord
remove all nils from an array
array.compact
return the maximum value from a hash
hash.max_by{|k,v| v}
return permutations of an array as an array
array.permutation.to_a
go through a loop, iterating by a number other than 1
0.step(0,50) {loop the code in here…}
declare a variable as a constant
Pi = 3.1415
return ASCII code for a character (not .ord)
?a, ?Z
regular expression meaning any two characters immediately after the beginning of a line
/^../
regular expression meaning the last two characters of a line
/..$/
regex first two characters of absolute start of a string
/\A../
regex last two characters of absolute end of a string
/..\Z/
iterate through a string and have access to each section of it
This is a test.scan(/../) {|x| puts x}
regex anchor for beginning of line
regex anchor for end of line
$
regex anchor for beginning of a string
\A
regex anchor for the end of a string
\Z
regex any character
.
regex any letter, digit, or underscore
\w
regex any character that \w doesn’t match
\W
regex any digit
\d
regex any non-digit
\D
regex whitespace
\s
regex non-whitespace
\S
regex match one or more of a certain type of character
+, e.g. string.scan(/\d+/) do |x| gives all numbers in a string
return first number of elements of an array, e.g. first 3 element
array.first(3)
return an array of all the keys of a hash
hash.keys
the ternary operator
?
.collect
Array#collect, iterates over the array with code block and returns new array
.strptime
DateTime#strptime