Liam Mackey's Projects

Cheat Sheet for Ruby Built In's

Array's

any?: Returns true if any element in the array passes the code block test.

count{|item|block}: Counts each item that passes the code block test. Returns integer.

cycle(num){|obj| block}: Cycles through the array a certain number of times or forever and applies a code block.

delete_if{|obj| block}: Deletes element if code returns true.

each_index{}: Iterates through the array by index value.

flatten: Turns multi demensional array's to single demensional

insert(value, index): Places value at specific index.

map{|obj|block}: Returns a new array with the values produced by the code block as it iterates through.

reverse_each{}: Iterates through the array in reverse.

sort_by{}: Sorts the array by the given code block.

Hashes

each_key{}: Iterates through the hash just using keys.

each_value{}: Iterates through the has using just values.

length: Returns the length of the hash

reject{}: Deletes pair if code block returns true.

Select{}: Keeps value if code block returns true.

Strings

str[index]: Returns the char at that index.

capitalize!: Capitalizes string.

concat() Concatenates two strings, if integer it takes the value as a code point

count(): Counts the amount of times a substring is prevelent in the string.

each_char{}: Applies a code block to every character.

each_line{}: Applies a code block to each line of the string.

gsub(pattern, replacemnet): Preforms a global substitution.

insert(index, str): Inserts a string at a specific index.

split(): Takes string and turns it into an array.

prepend(str): Adds string to the start of the string.

Numeric's and Enumerable's

abs2: Returns square of self.

ceil: Retrurns next integer that is equal to or greater than self.

floor: Retruns next integer that is less than or equal to self.

find_all{}: Returns all of the objects that are true.

group_by{}: Groups by the result of the code block.

inject(initial){|memo, obj| block}: Combines all of the objects in an enum and applies a binary operation, returns the result.

max_by{}: Returns the max out of the elements after a given code block is applied.

take_while{}: Returns all elements before the code block returns false.

first: Returns the first value.

last: Returns last value.

Helpful reminders

It is important to pseudocode throughly before starting to code. Make sure you understand what enumerator you are using. Each is not always the best or even viable one. Remember don't use a method unless you totally understand it. Good Luck!