Skip to content

Instantly share code, notes, and snippets.

View dissolved's full-sized avatar

Ryan Sandridge dissolved

View GitHub Profile
@dissolved
dissolved / danger_controller.rb
Last active August 29, 2015 14:01
What is the most destructive thing you could assign to params?
def string_to_camelcase(string)
string.gsub(/\s+/, "").camelize # camelize defined in Rails API
end
the_class = eval string_to_camelcase(params[:user_input])
the_object = the_class.new(params[:more_user_input],params[:even_more_user_input])
def self.select_options
@select_options ||= subclasses.map do |klass|
klass = klass.to_s
[klass.titleize, klass]
end
end
@dissolved
dissolved / after.erb
Created October 22, 2013 20:05
Wrapping a collection of objects in a Presenter more transparently. Expanding on ideas distributed by Jim Gay (http://clean-ruby.com/).
<ul>
<% agenda.sessions.each do |session| %>
<li>
<h3><%= session.title %></h3>
<p><%= session.other_view_method %></p>
</li>
<% end %>
</ul>
@dissolved
dissolved / gist:6945327
Created October 12, 2013 03:10
Why Arlington Ruby is so great!
[5:08pm] allie_p: is an instance variable and an instance method the same thing?
[5:09pm] saturnflyer: no
[5:09pm] saturnflyer: an instance variable is some internal value
[5:09pm] saturnflyer: an instance method is some function that object can perform
[5:10pm] allie_p: ok, so they're related but different
[5:10pm] saturnflyer: typically, you might keep the names the same to set and retrieve the instance variable values
[5:10pm] saturnflyer: attr_accessor creates 2 methods that set and retrieve the instance variable
[5:10pm] saturnflyer: but you could have a method called full_name, that returns something other than @full_name
[5:11pm] saturnflyer: it could be @__full_person_name, or whatever you want
[5:15pm] allie_p: thanks!
@dissolved
dissolved / functional_alt_gol.rb
Created September 30, 2013 01:50
Our solution to the following challenge: Conway's Game of Life without saving state and without using iteration (or anything provided by Enumerable). Also included is a shorter solution which does use Enumerable.
class NilClass
def [](index) end
end
class Array
def [](index)
(index < 0) ? nil : at(index)
end
end
@dissolved
dissolved / gist:6546370
Created September 13, 2013 03:00
Object.tap inconsistancy
{:mutable => false}.tap {|h| h[:mutable] = true}
# => {:mutable=>true}
"Can I mutate?".tap {|str| str = "No!"}
# => "Can I mutate?"
@dissolved
dissolved / gist:5794223
Created June 17, 2013 01:53
Ever want to go deep (or even "the end") of an infinite scroll? You could spend 10-30 minutes scrolling like a hamster... or you could use javascript to do it for you while you get a cup of java.
// To start scrolling, type this in the javascript console
var theTimeout=setInterval(function(){window.scrollTo(0, document.body.scrollHeight);},500);
// To stop scrolling, type this in the javascript console
clearInterval(theTimeout);
@dissolved
dissolved / gist:5463419
Created April 25, 2013 21:40
Work in progress for my .slate config
# Configs
config defaultToCurrentScreen true
config checkDefaultsOnLoad true
config keyboardLayout colemak
config windowHintsShowIcons true
config windowHintsIgnoreHiddenWindows true
config windowHintsSpread true
# Monitor Aliases
#dependencies
require "csv"
def import
filename = "pricing_sample.csv"
@file = CSV.open(filename, {:headers => true, :header_converters => :symbol})
end
def discrepancies
underscans = 0
@dissolved
dissolved / no_such_thing.rb
Created February 23, 2012 16:21 — forked from bokmann/no_such_thing.rb
The code of the talk from my Feb 22nd Arlington Ruby talk 'There is No Such Thing as Metaprogramming'.
# This is the code from my 'There is No Such Thing as Metaprogramming' talk,
# which premiered at the Arlington, VA Ruby Users Group on Feb 22nd.
# Without the deliver and walk-through to the solution below this example
# will be missing quite an important bit of content (mainly the tracking of
# 'self' while developing the solution, but it still a useful read.
# Here is the Toddler with no metajuju. Note that the developer, as well as
# the code, is completely unuaware of the interpreter. A developer with a
# background in compiled languages would be comfortable looking at this.