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.