Instantly share code, notes, and snippets.

View hanami_snippet.rb
# Example from: http://hanamirb.org/guides/1.2/getting-started/#implementing-create-action
module Web::Controllers::Books
class Create
include Web::Action
def call(params)
BookRepository.new.create(params[:book])
redirect_to '/books'
View frontend_api.rb
# frozen_string_literal: true
module Api
module V1
# Controller to consume read-only data to be used on client's frontend
class FrontEndController < ActionController::API
include Orderable
# GET /:resource
# GET /:resource.json
View after_commit_error.rb
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
View rbreadline_multibyte_test.rb
#coding: utf-8
# This will remove old Readline if it's already defined
require 'rb-readline'
# This will load RbReadline for real
require 'rbreadline'
# This will load Readline that relies on RbReadline
require 'readline'
View codeship_heroku_with_maintenance_mode.sh
#!/bin/sh
# This codeship custom script depends on:
# - API_KEY env var from Heroku
# - STAGING_APP_NAME
# - STAGING_APP_URL
set -e
export HEROKU_API_KEY="${API_KEY}"
View ruby_versions.md

Ruby Versions

Retired

  • 1.8.7 - Retired since 2013-06-30

  • 1.9.3 - Retired since 2015-02-23 - No bug fix, nor even security ones will be backported then

Security Maintenance Phase

View fg_find_or_create.rb
module FactoryGirl::Syntax::Methods
def find_or_create(name, attributes = {}, &block)
factory = FactoryGirl.factory_by_name(name)
clazz = factory.build_class
factory_attributes = FactoryGirl.attributes_for(name)
attributes = factory_attributes.merge(attributes)
clazz.find_or_create_by(attributes, &block)
end
View encoding.rb
file = File.open "acao_e_reacao_utf16_le.txt"
=> #<File:acao_e_reacao_utf16_le.txt>
file.methods.grep /enc/
=> [:external_encoding, :internal_encoding, :set_encoding]
file.external_encoding
=> #<Encoding:UTF-8>
file.internal_encoding
View arup_delete_three.rb
#!/usr/bin/env ruby
#coding: utf-8
def del_first_three_original_1(a)
num_to_del = a.find { |e| a.count(e) >= 3 }
return a if num_to_del.nil?
3.times do
ind = a.index { |e| e == num_to_del }
a.delete_at(ind)
end
View arup_meth_var.rb
#!/usr/bin/env ruby
# If no local variable is found,
# it begins the method lookup that ends calling
# the method_missing chain until the last one raises the error
begin
p undefined_name if true
rescue
puts "Raised an error"