Carbon-Neutral Rails
# Gemfile
gem 'patch_ruby'
require 'patch_ruby'
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem 'rails' |
# how to run: `$ ruby complex_arel_ordering_example.rb` | |
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem 'rails' |
# Gemfile
gem 'patch_ruby'
require 'patch_ruby'
{ | |
"mode": "patterns", | |
"proxySettings": [ | |
{ | |
"title": "EMR SOCKS Proxy", | |
"type": 3, | |
"color": "#cc8c2a", | |
"address": "localhost", | |
"port": 8157, | |
"proxyDNS": true, |
(defn average [x y] (* 0.5 (+ x y))) | |
(defn improve [guess x] (average guess (/ x guess))) | |
(defn square [x] (* x x)) | |
(defn abs [x] (if (< 0 x) x (- 0 x))) | |
(defn good-enough? [guess x] | |
(< (abs (- x (square guess))) 0.0000001)) |
def min_abs_sum(a) | |
n = a.size | |
[1, -1].repeated_permutation(n).map do |s| | |
val(a,s).abs | |
end.min | |
end | |
def val(a,s) | |
a.map.with_index{|e, i| a[i] * s[i]}.inject(:+) | |
end |
def cyclic_rotation(a, k) | |
size = a.size | |
return a if k == size || k == 0 | |
result = a.clone | |
a.each_with_index{|e, i| | |
result[(i + k) % size] = e | |
} | |
result | |
end |
# Returns the maximum binary gap of a number. | |
# e.g.: max binary gap of 529 (1000010001 in binary) is 4. | |
# | |
def max_binary_gap(n) | |
return 0 if (n < 1) | |
result = n.to_s(2).scan(/10+1/).map{|m| | |
m.gsub('1', '').size | |
}.select{|gap| | |
gap >= 0 |
-- see http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html MAPPING | |
WITH timezone_lookup(rails_timezone, pg_timezone) | |
AS ( | |
VALUES | |
('International Date Line West', 'Pacific/Midway'), | |
('Midway Island', 'Pacific/Midway'), | |
('American Samoa', 'Pacific/Pago_Pago'), | |
('Hawaii', 'Pacific/Honolulu'), | |
('Alaska', 'America/Juneau'), | |
('Pacific Time (US & Canada)', 'America/Los_Angeles'), |
pragma solidity ^0.4.17; | |
contract Inbox{ | |
string public message; | |
function Inbox(string initialMessage) public { | |
message = initialMessage; | |
} | |
function setMessage(string newMessage) public { |