Skip to content

Instantly share code, notes, and snippets.

@loganhasson
loganhasson / amazon-uploader.rb
Created June 24, 2014 17:02
amazon uploader class
class AmazonUploader
attr_reader :category_name, :media_type, :file, :filename, :bucket
def initialize(media_object, file)
@category_name = media_object.category.bucket_name
@media_type = media_object.class.to_s.downcase + "s"
@bucket = 'iron-uploads'
@file = file.tempfile
@filename = file.original_filename
end
@loganhasson
loganhasson / cl-github-repo.md
Created June 4, 2014 19:39
better command line github repo creation

Add to your .bash_profile:

# A function to create new github repos from the command line
# USE: gh repo-name

function gh () {
  curl -s -u '<githubusername>:<githubapitoken>' https://api.github.com/user/repos -d "{\"name\":\"$1\"}" | sed -n '/"ssh_url"/p' | gawk 'match($0, /:{1}\s"(.*)"/, ary) {print ary[1]}' | pbcopy
}
@loganhasson
loganhasson / setup-some-servers.md
Created May 22, 2014 14:58
Let's set up a cool cluster of Ubuntu 12.04LTS 64-bit servers.

Note: All steps assume you are running Ubuntu 12.04LTS 64-bit servers. Most of these steps should apply to any distribution of Ubuntu, however.

App Servers

Steps

  1. apt-get update
  2. apt-get upgrade
  3. apt-get install build-essential
  4. apt-get install sqlite3 libsqlite3-ruby libsqlite3-dev

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
def self.make_json
# just gotta do this cuz of lame scope stuff
path = nil
# set up some arrays for my node hashes and my link hashes
nodes = []
links = []
# just for figuring out when to change "groups" (not important aside from coloring in D3.js
group = 1
@loganhasson
loganhasson / Sieve of Eratosthenes.md
Last active December 31, 2020 20:48
A Ruby example of using the Sieve of Eratosthenes to determine whether or not a number is prime

Using the Sieve of Eratosthenes to Find Prime Numbers in Ruby

The Sieve of Eratosthenes is a super efficient way to determine whether or not numbers are prime. Essentially, it takes a max number, and via the process of elimination, gives you an array that contains every prime number up to that max number.

The very short explanation is this:

  1. Create an array from 0..max
  2. Starting at 2, delete every multiple of 2 from the array.
  3. Then, go back to the beginning, and delete every multiple of 3.
  4. Repeat this starting from the next available number at the beginning of the array.
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
.explore {
pointer-events: none;
font-family: "klinicslabmedium", "Memphis", Georgia, serif;
font-weight: normal;
filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#fccb45', EndColorStr='#ff5208', gradientType='1');
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0.21, rgb(104, 206, 192)),
<!DOCTYPE html>
<!--[if IE 7]>
<html class="no-js ie ie7" lang="en-US">
<![endif]-->
<!--[if IE 8]>
<html class="no-js ie ie8" lang="en-US">
<![endif]-->
<!--[if IE 9]>
<html class="no-js ie ie9" lang="en-US">
class MontyHall
attr_accessor :choice_index, :doors, :choice
attr_reader :switch_opt
@@win_count = 0
@@loss_count = 0
def initialize(switch_opt)
@doors = ["Car", "Goat", "Goat"].shuffle