DEV Community

Cover image for Integer, Float, and Array class
Felix Imbanga
Felix Imbanga

Posted on

Integer, Float, and Array class

Integer class
/ when dividing only returns the whole
% returns the remainder
Rubyists tend to end method names with a question marks when methods return true or false.
rand returns a random number. rand(n) will return a random integer between 0 and n-1 rather than 1 and n
object#method is different from object.method
.to_f is useful in converting an integer to a float.

Float class
Division with floats works the way we're used to. It returns fractional results, as a float.
If either number when dividing with \ is a float, float division will be performed.
rand can also be called without methods.
push == << for shorthand.

Array class
.at method in an array refers to the index, which starts at 0. nil represents absence of in index in the array aka doesn't exist.
In an array, to count backwards through a list of items, start with -1 (aka the last item)
.at == object[index] shorthand.
.index method returns the index where it resides
.split will return an array of substrings
.split with an argument tells the program to split at the character of the argument.
.spliting with an empty argument ("") turns a string into an array of individual characters.
.count counts how many items are in a list if called with no arguments. If an argument is provided it counts how many times that argument occurs in the list.
.sample returns an unpredicted item from an array

Top comments (0)