Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / array.rb
Created February 5, 2015 16:07
Ruby core extension for adding items conditionally to Arrays
class Array
# Inserts an object to the Array if condition is true
#
# object - The object to insert.
# condition - The condition to evaluate.
#
# Example:
#
# = content_tag(:div, class: ["user-username"].insert_if("active", @user.active)) do
@Bodacious
Bodacious / example.rb
Created January 21, 2015 12:50
Exposable attribute
class OrdersController
def index
end
def pending
end
private
@Bodacious
Bodacious / example_controler.rb
Last active August 29, 2015 14:13
controller/views/intsance varialbes
# Bad:
class OrdersController
exposes :orders
def index
@orders = Order.complete
end
def pending
@Bodacious
Bodacious / benchmark.rb
Created August 6, 2014 11:29
Benchmarking Memoizing of Ruby Methods
require 'benchmark'
class MyClassWithStuff
DEFAULT_VALS = { one: '1' }
def memoized_fetch
@value ||= DEFAULT_VALS[:one]
end
@Bodacious
Bodacious / app_delegate.rb
Created April 1, 2014 20:00
Rubymotion developers often seem to shy away from using getter methods for instance variables. Instead, they're defined all over the place - as you'd see more in Objective-C.
# This is the approach I see most commonly in Rubymotion code examples.
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@mainViewController = MainViewController.alloc.init
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame)
@window.rootViewController = mainViewController
@window.makeKeyAndVisible
true
end
@Bodacious
Bodacious / musical_note_frequencies.rb
Last active January 2, 2016 14:59
List of musical notes and their frequencies calculated in Ruby
# The freqencies of A are easiest as most are integers
# Remember - each octave means double frequency
A_FREQS = %w[ 27.5 55 110 220 440 880 1760 3520 7040 14080 ].map(&:to_f)
# The names of the 12 semitones
NOTES = %W[ A\s A# B\s C\s C# D\s D# E\s F\s F# G\s G# ]
A_FREQS.each_with_index do |freq, octave|
NOTES.each_with_index do |note, i|
octave += 1 if note == "C\s"
# app/helpers/forms_helper.rb
module FormsHelper
# A custom FormBuilder class for Rails forms with Twitter Bootstrap 3
class CustomFormBuilder < ActionView::Helpers::FormBuilder
# ActionView::Helpers::FormHelper and ActionView::Helpers::FormBuilder
# methods each have different args. This hash stores the names of the args
# and the methods that have those args in order to DRY up the method aliases
# defined below.
@Bodacious
Bodacious / application_helper.rb
Created November 8, 2013 18:18
Rails helpers for paypal payment acceptance methods
def paypal_acceptance_badge
path = "https://www.paypal.com/webapps/mpp/paypal-popup"
im_path = "https://www.paypalobjects.com/webstatic/mktg/logo/AM_SbyPP_mc_vs_dc_ae.jpg"
link_to(path, title: "How PayPal Works", id: "paypal_acceptance_badge") do
image_tag(im_path, border: "0", alt: "PayPal Acceptance Mark")
end
end
=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')
@Bodacious
Bodacious / pesapal.rb
Created May 22, 2013 09:52
PesaPal Implementation in Ruby (test)
module PesaPal
require 'active_support'
include ActiveSupport::Configurable
self.configure do |config|
config.test_mode = true
config.consumer_key = "<your key>"
config.consumer_secret = "<your secret>"
end