Ruby Methods Flashcards
What is the ruby method that converts a non-string element into a string?
.to_s
What is the ruby method that converts a non-INTEGER element into an integer?
.to_i
What is the ruby method that converts a string into all capital letters?
.upcase
What is the ruby method that converts a string into all lowercase letters?
.downcase
What is the ruby method that changes the case of each character in a string?
.swapcase
What is the ruby method that capitalizes the first character of a string?
.capitalize
.capitalize will capitalize:
A. The first letter of a string
B. The first character of a string
C. All characters in the string
B. The first character of a string (So, if the string starts with a space, no element will be capitalized)
What does the method == mean?
BOOLEAN OPERATOR: Evaluates if elements are equal to one another. Returns TRUE or FALSE.
What does the method || mean?
BOOLEAN OPERATOR: Evaluates if A or B are true. Returns TRUE or FALSE.
What does the method && mean?
BOOLEAN OPERATOR: Evaluates if A and B are true. Returns TRUE or FALSE.
.even?
Test whether an integer is even. Returns true or false.
.abs
Absolute value for an integer.
num = -1234 # variable assignment positive = num.abs # => 1234
p
Writes to console. This works like puts but displays values such as nil explicitly.
Example:
p “Jonathan”
Public Methods
Can be called by anyone—no access control is enforced. Methods are public by default (except for initialize, which is always private).
Protected Methods
Can be invoked only by objects of the defining class and its sub- classes. Access is kept within the family.
If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.
Private Methods
Cannot be called with an explicit receiver—the receiver is always the current object, also known as self. This means that private methods can be called only in the context of the current object; you can’t invoke another object’s private methods.
If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.
.dup
Used to duplicated objects. Does not work for numbers, but does work for strings, hashes, arrays, and ranges.
Can be used on variables to avoid aliasing.
.freeze
Used to freeze objects. This means it prevents modifications to the object. It does not, however, prevent re-assigning.
Example:
a = [4,5,6]
a. freeze
a. pop
=> RuntimeError: can’t modify frozen Array
a = [4,5,6] #reassigns
a.pop
6 # popped the 6 from the end
a # calls variable
[4,5] #returns minus the popped digit
.scan
Scan RETURNS AN ARRAY of substrings that match a given pattern.
Example using Regex:
string.scan(/[\w’]+/)
.sort_by
Takes a code block. Not an iterator, but just a block telling the code what to sort_by.
Example:
%w(a aa aaaaaaaaaa aaa).sort_by { |word| word.length}
returns:
[“a”, “aa”, “aaa”, “aaaaaaaaaa”]
.each
The method each is an iterator—a method that invokes a block of code repeatedly.
Example:
[1,2,3,4,5,6].each do |i|
puts “I am the #{i} iteration.”
end
I am the 1 iteration. I am the 2 iteration. I am the 3 iteration. I am the 4 iteration. I am the 5 iteration.
.find
Find takes a block that tells Ruby what do find in an array.
Example:
[1,3,5,7,9].find{|v|v*v>30}
If no element is found, it returns nil.
=> 7
It only finds the first occurernce that is true. To find all, you’d have use find_all.
.find_all
Find_all takes a block that tells Ruby what do find in an array.
Example:
[1,3,5,7,9].find_all{|v|v*v>30}
If no element is found, it returns AN EMPTY ARRAY, not nil.
=> [7,9]
.succ
The succ method increments a string value.
Example:
“a”.succ # b
Also works on numbers:
456.succ # 457