Skip to content

Instantly share code, notes, and snippets.

@MartinHeimbring
MartinHeimbring / Serialize ruby objects
Created April 3, 2015 19:40
Automate creating a #to_json method for an object (in order to serialize the properties) by inheriting from a class that does exactly this
class JSONable
# serialize object to json
def to_json
hash = Hash.new
self.instance_variables.each do |ivar|
hash[ivar] = self.instance_variable_get ivar
end
hash.to_json
end
@MartinHeimbring
MartinHeimbring / error messages partial
Last active August 29, 2015 14:15
Form error messages w/ bootstrap (from MIchael Hartl Tutorial)
<!-- _error_messages.html.erb -->
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
# add nokogiri gem to Gemfile
form_fields = [
'textarea',
'input',
'select'
]
@MartinHeimbring
MartinHeimbring / bootstrap flash_messages partial
Created February 15, 2015 11:16
associate right bootstrap class with flash key
- flash.each do |type, message|
%div{ class: "alert #{bootstrap_class_for(type)}" }
%button.close{ :data => { :dismiss => "alert" } } x
= message
# From application helper
def bootstrap_class_for flash_type
#require 'rubygems'
require 'pp'
#require 'ap' # Awesome Print
class Object
# expects [ [ symbol, *args ], ... ]
def recursive_send(*args)
args.inject(self) { |obj, m| obj.send(m.shift, *m) }
end
end
@MartinHeimbring
MartinHeimbring / Benchmark: Lookup time (string vs.symbol as key)
Created February 8, 2015 14:32
Using the Benchmark module to compare lookup time in hash
require 'benchmark'
stringAZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
symbolAZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]
string_time = Benchmark.realtime do
100_000.times{stringAZ["r"]}
end
symbol_time = Benchmark.realtime do
@MartinHeimbring
MartinHeimbring / The & Operator in Ruby
Created January 29, 2015 16:03
Different ways to use the & operator for everyday operations
###################################################
# calling a proc on a collection of numbers
increment_by_1 = proc { |n| n + 1 }
# the & converts the proc into a block that is then invoked on each item in the collection
[1,2,3].map(&increment_by_1)
# returns [2, 3, 4]
###################################################
# calling a lambda on a collection of numbers
incrememnt_by_1 = ->(n){n+1}
@MartinHeimbring
MartinHeimbring / Yield Self example
Last active August 29, 2015 14:14
Yield Self in initialize
# No yielding
class Person
attr_accessor :first, :last
def initialize(first = nil, last = nil)
@first = first
@last = last
end
def hello
puts "#{@first} #{@last} says hello!"
@MartinHeimbring
MartinHeimbring / Bootstrap 3 Navbar Haml
Created January 28, 2015 13:26
Bootstrap 3 Navbar Haml
%nav.navbar.navbar-default{role: "navigation"}
/ Brand and toggle get grouped for better mobile display
.navbar-header
%button.navbar-toggle{"data-target" => ".navbar-ex1-collapse", "data-toggle" => "collapse", type: "button"}
%span.sr-only Toggle navigation
%span.icon-bar
%span.icon-bar
%span.icon-bar
%a.navbar-brand{href: "#"} Brand
/ Collect the nav links, forms, and other content for toggling
@MartinHeimbring
MartinHeimbring / Devise session form bootstrap
Created January 19, 2015 21:19
Devise session form styled with bootstrap
%h2.text-center Sign in
%br/
= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: 'form-horizontal'}) do |f|
.form-group
= f.label :email, class: "col-sm-2 control-label"
.col-sm-6
= f.email_field :email, autofocus: true , class: "form-control"
.form-group
= f.label :password, class: "col-sm-2 control-label"
.col-sm-6