Skip to content

Instantly share code, notes, and snippets.

Thing.find_by!(id: 123, state: "pending").state #=> "approved"
class SloppyHash
def [](k)
memory[k] ||= SloppyHash.new
end
def []=(k,v)
memory[k] = v
end
module RecursiveKeyCheck
def recursive_key?(key)
return true if key?(key)
self.each do |_, v|
if v.is_a?(Hash)
return true if v.recursive_key?(key)
end
end
$('#btnbtnbtn').on('click', function(e) {
var res = $('#res').val();
var sum= $('#sumThis').val();
var ress=res+sum;
$('#res').val(ress);
})
module Routedraw
def self.define_api_routes(routes: Rails.application.routes,
version: :v1,
&block)
routes.draw do
namespace :api, defaults: { format: :json } do
namespace version do
# yield self to pass the current context to the block
yield self
end
@bricker
bricker / f.rb
Created February 21, 2015 00:49
(1..5).each do |i|
f = File.open("somefile#{i}.txt")
begin
do_stuff_with(f)
rescue
next
ensure
f.close
end
public class Person {
private String mName;
public Person() {}
public void setName(String mName) { this.mName = mName; }
}
require 'spec_helper'
shared_examples "extended_definition" do
it "must contain original definition" do
expect(sense.extended_definition).to include(sense.definition)
end
it "must contain both sample texts" do
expect(
sense.samples.pluck(:sample).collect { |t| sense.extended_definition.include? t }.inject(:&)
{
...
:user_hash => Proc.new { |current_user|
OpenSSL::HMAC.hexdigest(
"sha256",
"ENV['intercom_secret']",
current_user.id.to_s
)
},
...
require 'rails_helper'
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from Pundit::NotAuthorizedError, with: :permission_denied
rescue_from ActionController::UnknownFormat, with: :unknown_format
private
def permission_denied