Skip to content

Instantly share code, notes, and snippets.

View kenichi's full-sized avatar

Kenichi Nakamura kenichi

  • Portland, Oregon
View GitHub Profile
require 'sinatra/base'
class App < Sinatra::Base
get '/' do
"hello!\n"
end
post '/exit' do
Thread.new { sleep 1; exit }
"exiting!\n"
end
require 'newrelic_rpm'
class Err
def self.do
raise 'here is an error'
end
end
desc 'test notice error'
task :error do
def bam(*a, baz:, bat:, **h, &block)
[*a, block.parameters, baz: baz, bat: bat, **h]
end
__END__
> bam 1, 2, 'a', 'b' => 'c', d: 0xe, baz: 'mno', bat: 'jkl' do |*ar, foo:, **hash|; end
=> [1, 2, "a", {"b"=>"c"}, [[:rest, :ar], [:keyreq, :foo], [:keyrest, :hash]], {:baz=>"mno", :bat=>"jkl", :d=>14}]
require 'newrelic_rpm'
class A
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
# if this was removed, B.foo would not be instrumented, because B's definitiion overrides #foo
def self.inherited subclass
subclass.add_method_tracer :foo
end

First, I switch to JRuby 1.7 running in 1.9 mode

knakamura:test $ chruby jruby-1.7
knakamura:test $ wr
/Users/knakamura/.rubies/jruby-1.7.25/bin/ruby
jruby 1.7.25 (1.9.3p551) 2016-04-13 867cb81 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_102-b14 +jit [darwin-x86_64]

Then I write out a simple Gemfile, no ruby directive

NoMethodError: Couldn't find Member with [WHERE "members"."deleted_at" IS NULL AND (lower(sponsor_id) = '22403602')]
$ rails new asset_hosted 2>&1 1>/dev/null
$ cd asset_hosted
$ echo 'Rails.application.config.action_controller.asset_host = "example.com"' >> config/initializers/assets.rb
$ be rails runner 'puts ApplicationController.asset_host'
Running via Spring preloader in process 56593
example.com
Rails.application.configure do
# ...
config.action_controller.asset_host = Proc.new do |source|
if source.ends_with?(".jpg") || source.ends_with?(".gif") || source.ends_with?(".png")
"https://cdn-images.example.com"
else
"https://cdn.example.com"
end
end
@kenichi
kenichi / ruby-2.3.3.txt
Last active November 30, 2016 00:23
bizzarro - the difference is just the .**(2) or .** 2
$ chruby 2.3.3
$ irb
irb(main):001:0> [].concat [1,2,3].map {|x| x.** 2}.select do |x|; x%2==0; end
=> [4]
irb(main):002:0> [].concat [1,2,3].map {|x| x.**(2)}.select do |x|; x%2==0; end
TypeError: no implicit conversion of Enumerator into Array
from (irb):2:in `concat'
from (irb):2
from /Users/knakamura/.rubies/ruby-2.3.3/bin/irb:11:in `<main>'
@kenichi
kenichi / io_nonblock.rb
Last active November 23, 2016 02:12
TIL - IO#*_nonblock take exception: kwarg
require 'benchmark/ips'
i, o = IO.pipe
rs = [i]
Benchmark.ips do |x|
x.report :exceptions do
begin
i.read_nonblock 1