⌘T | go to file |
⌘⌃P | go to project |
⌘R | go to methods |
⌃G | go to line |
⌘KB | toggle side bar |
⌘⇧P | command prompt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class House | |
def initialize(temperature = 68, min_temp = 58, max_temp = 80) | |
@temperature = temperature | |
@max_temp = max_temp | |
@min_temp = min_temp | |
@heater = "off" | |
@air_conditioner = "off" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cities = {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville'} | |
cities['NY'] = 'New York' | |
cities['OR'] = 'Portland' | |
def find_city(map, state) | |
if map.include? state | |
return map[state] | |
else | |
return "Not found." |
The Strategy pattern is a delegation-based approach to solving the same problem as the Template Method pattern -- instead of breaking up the variable parts of your algorithm and implementating those parts in subclasses, you simply implement each version of your algorithm as a separate object. You can then vary the algorithm by supplying different strategy objects to the context.
The Strategy Pattern consists of strategies, which are interchangeable classes which encapsulate varieties of a particular algorithm, and the context class, which utilizes strategies. The context can choose different strategies depending on the situation.
Here are a couple code examples illustrating the Strategy Pattern:
# strategy class
- Download this application skeleton.
- Convert the app to use AJAX.
- Add any files you changed to your gist and submit your code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the element, add a click listener... | |
document.getElementById("parent-list").addEventListener("click",function(e) { | |
// e.target is the clicked element! | |
// If it was a list item | |
if(e.target && e.target.nodeName == "LI") { | |
// List item found! Output the ID! | |
alert("List item " + e.target.id.replace("post-", "") + " was clicked!"); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="tours"> | |
<h1>Guided Tours</h1> | |
<ul> | |
<li class="usa tour" data-discount="299"> | |
<h2>New York, New York</h2> | |
<span class="details">$1,899 for 7 nights</span> | |
<button class="book">Book Now</button> | |
</li> | |
<li class="europe tour" data-discount="176"> | |
<h2>Paris, France</h2> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.tour{ | |
background-color: green; | |
border-radius: 10px; | |
} | |
h2{ | |
margin-top: 5px; | |
margin-left: 15px; | |
} | |
.details{ | |
margin-left: 15px; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="tour"> | |
<h2>Paris, France Tour</h2> | |
<p>$2,499 for 7 Nights</p> | |
<button>See photos from our last tour</button> | |
<ul class="photos"> | |
<li> | |
<img src="/assets/photos/paris1.jpg"> | |
<span>Arc de Triomphe</span> | |
</li> | |
<li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('#tour').on('click', 'button', function() { | |
$('.photos').slideToggle(); | |
}); | |
function showPhotos() { | |
$(this).find('span').slideToggle(); | |
} | |
OlderNewer