Skip to content

Instantly share code, notes, and snippets.

@LNA
LNA / java_basics.java
Last active August 29, 2015 13:56
Java Basics
// What is an instance variable?
// An instance variable is the variable declared inside of the class but outside of the method.
class Shoe {
// these are all instance variables:
public String shoeBrand;
public String[] shoeSeason;
public int shoeSize;
// getters and setters follow
@LNA
LNA / old_ttt_board.rb
Last active August 29, 2015 13:56
Bad TTT Board
<table class="board">
<tr>
<td>
<div id="0" class="square" >
<input type="submit" name="square" value="0">
</div>
</td>
<td>
<div id="1" class="square" >
<input type="submit" name="square" value="1">
@LNA
LNA / better_ttt_board.rb
Created February 13, 2014 20:36
Better TTT Board
<ul class="board clearfix">
<% count = 0 %>
<% 9.times do %>
<li id="<%= count %>" class="square">
<input type="submit" name="square" value="<%= count %>">
</li>
<% count += 1 %>
<% end %>
</ul>
@LNA
LNA / 0_reuse_code.js
Created February 14, 2014 21:51
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@LNA
LNA / clues.rb
Last active August 29, 2015 13:56
Clues In Test Driving Solutions
it “returns first n numbers” do
# I know there are a few ways to approach this. I could chose a while loop and use a counter to track how many indexes I have counted through. However, I would go for a range. It gets the job done with less code. Less code is good.
end
it “returns a portion of a given group in the form of an array if some condition is met” do
# use find_all.
end
it “returns two arrays: one for a false condition” and it “returns two arrays: one for a true condition” do
# use partition.
@LNA
LNA / interface_example.rb
Last active August 29, 2015 13:56
Example of Public versus Private Interface In Ruby
class GameState
# omitted code
def winner
[first_row,
second_row,
third_row,
left_column,
middle_column,
@LNA
LNA / ugly_code.rb
Last active August 29, 2015 13:57
Violating Encapsulation
class MutualMentions
attr_accessor :file, :relevant_tweets
def initialize(file)
@file = file
@relevant_tweets = [] # Red flag
end
def tweets_involving(user)
file = File.readlines(@file)
@LNA
LNA / encapsulated_code.rb
Last active August 29, 2015 13:57
Better Looking Code
class MutualMentions
attr_accessor :file, :first_user, :second_user
def initialize(file, first_user, second_user)
@file = file
@first_user = first_user
@second_user = second_user
end
def tweets_involving(user)
@LNA
LNA / before_refactor.rb
Last active August 29, 2015 13:57
Simple Refactor
def received(tweets)
tweets.each do |tweet|
if tweet.receiver == name
@received_tweets << tweet
end
end
@received_tweets
end
@LNA
LNA / hartl_fix.rb
Created March 26, 2014 15:54
Listing 5.26 will make all of your specs fail.
SampleApp::Application.routes.draw do
root 'static_pages#home'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
#should be
SampleApp::Application.routes.draw do