Skip to content

Instantly share code, notes, and snippets.

View nastysloper's full-sized avatar
💭
Climbing Rocks, Running Trails, Writing Code

Richard Vogt nastysloper

💭
Climbing Rocks, Running Trails, Writing Code
  • Accenture Federal Services
  • San Antonio, Texas
View GitHub Profile

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@nastysloper
nastysloper / house.rb
Last active December 16, 2015 14:08
I'm refactoring code for the House class to use cleaner Boolean expressions.
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
@nastysloper
nastysloper / hash_ex.rb
Created May 3, 2013 15:26
Hello, all! This exercise is from Zed Shaw's Learn Ruby the Hard Way, and there are a few points that seem mysterious. On line 22, it seems that a hash object is calling a method ".call" with two arguments, the original hash, and a string the user has entered. I know the code works, and I know the code can be simpler. I'm just wondering what sor…
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."

Strategy Pattern

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

Instructions:

  1. Download this application skeleton.
  2. Convert the app to use AJAX.
  3. Add any files you changed to your gist and submit your code.
// 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!");
}
});
@nastysloper
nastysloper / demo.html
Created June 9, 2013 16:42
Node manipulation using (this) keyword http://jsfiddle.net/6w5mx/2/
<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>
@nastysloper
nastysloper / highlight.js
Last active December 18, 2015 07:09
successively refactored jQuery code...
.tour{
background-color: green;
border-radius: 10px;
}
h2{
margin-top: 5px;
margin-left: 15px;
}
.details{
margin-left: 15px;
<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>
$(document).ready(function() {
$('#tour').on('click', 'button', function() {
$('.photos').slideToggle();
});
function showPhotos() {
$(this).find('span').slideToggle();
}