Skip to content

Instantly share code, notes, and snippets.

View jonahoffline's full-sized avatar

Jonah Ruiz jonahoffline

View GitHub Profile
@jonahoffline
jonahoffline / strong_parameters_example.rb
Created December 26, 2012 06:25
Rails 4 example of strong_parameters. This is the new way of doing controllers and whitelisting parameters.
class HeadlinesController < ApplicationController
before_action :set_headline, only: [:show, :edit, :update, :destroy]
# GET /headlines
# GET /headlines.json
def index
@headlines = Headline.all
respond_to do |format|
format.html # index.html.erb
@jonahoffline
jonahoffline / geocoder_example.rb
Created December 26, 2012 16:43
Example use of the 'geocoder' gem.
require 'geocoder'
class Locator
def search(address)
Geocoder::coordinates(address)
end
end
#Locator.new.search('san juan, pr')
@jonahoffline
jonahoffline / app.rb
Last active December 10, 2015 08:08
Sinatra app.rb fix for jacobbednarz (original gist: https://gist.github.com/00279c2749e202a82d6d).
require 'sinatra/base'
class App < Sinatra::Base
before do
@images = [
{ title: "matrix", url: "http://i0.kym-cdn.com/entries/icons/original/000/009/889/Morpheus2.jpg" },
{ title: "google", url: "http://i0.kym-cdn.com/news_feeds/icons/original/000/006/501/google-search.jpg" },
{ title: "skywalking", url: "http://i2.kym-cdn.com/photos/images/newsfeed/000/295/090/9de.jpg" }
]
end
@jonahoffline
jonahoffline / revX_refactoring_example.rb
Last active December 11, 2015 01:59
Refactoring examples using code taken from revXbot.
#Original
# If you were down with this you are a code martyr waiting for a stab.
# Ruby was born so that code could be drier than your Grandma.
# The first any? method is as useless as a PHP Senior Developer so I ain't worried!
# The @offenders hash always get saved using the store method so cut that bitch off!
if @offenders.any? && @offenders.key?(nick)
@offenders.store(nick, @offenders.fetch(nick).next)
else
@offenders.store(nick, 1)
@jonahoffline
jonahoffline / spam_bitch_refactoring.rb
Created February 7, 2013 02:07
Refactoring examples using code taken from SpamBitch.
# Original
class Spammer
def initialize
@random_telephones = Proc.new { "787-#{rand(000..999)}-#{rand(0000..9999)}" }
@sms_telephones = Proc.new { "787#{rand(000..999)}#{rand(0000..9999)}" }
end
def telephone
@random_telephones.call
end
@jonahoffline
jonahoffline / crypt_example.rb
Created February 13, 2013 13:04
Crypt example.
class CustomAuth
def initialize(secret)
@secret = secret
end
def encrypt(key)
@secret.crypt(key)
end
def decrypt(key)
@jonahoffline
jonahoffline / fancy_pry.rb
Created March 2, 2013 10:15
For emulating fancy_irb's inline results with Pry. Thanks to cirwim at #pry who made this!
_pry_.print = lambda { |o, v| o.print "\e[1A\e[18C # => "; o.puts v.inspect }
@jonahoffline
jonahoffline / .irbrc.rb
Created March 7, 2013 22:08
My .irbrc configuration file
#!/usr/bin/env ruby
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
def y(obj)
@jonahoffline
jonahoffline / wele_stack.rb
Last active December 15, 2015 02:39
WeleStack Queue example.
require 'forwardable'
class WeleStack
extend Forwardable
def initialize(obj=[])
@queue = obj
end
def_delegator :@queue, :push, :empuja
@jonahoffline
jonahoffline / person.rb
Created March 21, 2013 03:00
Using Struct to create a class.
class Person < Struct.new(:name, :email)
def info
"#{self.name} #{self.email}"
end
def first_name
clean_name.first
end
def middle_name