This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OrdersController | |
| def index | |
| end | |
| def pending | |
| end | |
| private | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Bad: | |
| class OrdersController | |
| exposes :orders | |
| def index | |
| @orders = Order.complete | |
| end | |
| def pending |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'benchmark' | |
| class MyClassWithStuff | |
| DEFAULT_VALS = { one: '1' } | |
| def memoized_fetch | |
| @value ||= DEFAULT_VALS[:one] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| =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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |