Skip to content

Instantly share code, notes, and snippets.

View nathanmpark's full-sized avatar

Nathan Park nathanmpark

View GitHub Profile
@nathanmpark
nathanmpark / Enumerable#cycle
Last active September 23, 2015 16:29
Enumerable#cycle Example
a = ["Ferrari","Mercedes","BMW"]
a.cycle { |x| puts x }
# prints: Ferrari, Mercedes, BMW, Ferrari, Mercedes, BMW,.. forever.
a.cycle(2) { |x| puts x }
# prints: Ferrari, Mercedes, BMW, Ferrari, Mercedes, BMW.
# The general structure of the cycle enumerable is as such:
cycle(n=nil){|obj| block} -> nil
# n = the number of times you want to cycle through the list of data.
# obj = object that is being passed into cycle.
# block = a block of code you wish to run onto the given object.
# An enumerator can be created as well:
class Bike
def initialize
@owner = "Nathan Park"
end
def wheel_size
@wheel = 26
end
def bike_frame
def tire_pressure # this is an example of an instance method
@psi = 100 # instance variables are signified with the @ symbol
end
// Example JavaScript Object
var skateboarder = {
name: "Rodney Mullen",
board: "Almost Deck",
wheels: "Spitfire",
trucks: "Independent"
};
// The object must be initialized with the keyword "var" and contain the properties within the curley brackets.
// This example shows the object "skatboarder".
// Add property to object
skateboarder.trick = "360 Flip";
var skateboarder = {
name: "Rodney Mullen",
board: "Almost Deck",
wheels: "Spitfire",
trucks: "Independent",
trick: "360 Flip" // The new property trick is added here
};
// Delete property from object
delete skateboarder.trucks;
var skateboarder = {
name: "Rodney Mullen",
board: "Almost Deck",
wheels: "Spitfire",
trick: "360 Flip" // The old property trucks is deleted
};
grades = {
Jane: 89,
Bill: 88,
James: 95,
Jill: 94
}