Skip to content

Instantly share code, notes, and snippets.

@allard
allard / to_b.rb
Created May 2, 2023 00:02
to_b makes boolean evaluation more deterministic and dependable.
# install in rails: config/initializers/to_b.rb
#
# Examples of how it makes things easier:
#
# true if [] # => true
# true if [].to_b # => nil
# true if "" # => true
# true if "".to_b # => nil
# true if 0 # => true
# true if 0.to_b # => nil
@allard
allard / pick_first.rb
Created February 9, 2022 01:16
Ruby function to help pick the first available value.
# config/initializers/pick_first.rb in a rails app
class Object
# The pick_first function is a replacement for statements like users_value.present? ? users_value : default_value
# which using pick first can be replaced with pick_first users_value, default_value
# This is particularly helpful when you start getting into selecting the first out of three or more
# available values.
#
# pick_first "x", "y", "z" # => "x"
# pick_first nil, "y", "z" # => "y"
@allard
allard / bootstrap.rb
Created May 13, 2012 01:04 — forked from jamiepenney/bootstrap.rb
Form builder for Twitter Bootstrap v2 form elements
# This file goes in config/initializers
require 'bootstrap_form_builder'
# Make this the default Form Builder. You can delete this if you don't want form_for to use
# the bootstrap form builder by default
ActionView::Base.default_form_builder = BootstrapFormBuilder::FormBuilder
# Add in our FormHelper methods, so you can use bootstrap_form_for.
ActionView::Base.send :include, BootstrapFormBuilder::FormHelper