Skip to content

Instantly share code, notes, and snippets.

@sugamasao
sugamasao / main.go
Created February 22, 2019 11:54
YAMLデータのキーを変数経由で指定したい
package main
import (
"io/ioutil"
"log"
"reflect"
yaml "gopkg.in/yaml.v2"
)
@sugamasao
sugamasao / gem_list.rb
Created December 25, 2018 11:12
installed gem list
pp Dir.children(File.join(Gem.dir, 'gems'))
@sugamasao
sugamasao / sample.rb
Created May 14, 2018 08:44
requireしたファイルと現実のファイルの差分を得る
# こんな感じでいけそうなんだけど、どうか
at_exit {
path = File.join(File.expand_path(__dir__))
require_files = $LOADED_FEATURES.select{|f| f.start_with?(path)}
files = Dir.glob("./**/*.rb").map{|f| File.expand_path(f)}.select{|f| f.start_with?(path)}
diff = files - require_files
puts "*** not required files(#{ diff.size }) ***"
puts diff
}
@sugamasao
sugamasao / default_sample.rb
Created April 6, 2018 02:02
hash.default
irb(main):001:0> foo = {}
=> {}
irb(main):002:0> foo.default = "a"
=> "a"
irb(main):003:0> foo[:z]
=> "a"
irb(main):004:0> foo[:z].upcase!
=> "A"
irb(main):005:0> foo[:x]
=> "A"
@sugamasao
sugamasao / require_sample.rb
Created February 26, 2018 08:02
標準ライブラリをrequireしたい
# こんな感じでできそうだけど、内容があってるかは確認してない
Dir.glob(File.join(RbConfig.expand("$(libdir)"), 'ruby', "#{ RUBY_VERSION.split('.')[0, 2].join('.') }.0", "*.rb")).each do |path|
name = File.basename(path, ".rb")
next if %w(profile debug).include? name # こいつらをrequireするとプログラムが色々動いてしまうので除外
require name
end
@sugamasao
sugamasao / each_cause_sample.rb
Created February 6, 2018 15:28
each_causeみたいなのがほしい
class StandardError
def each_cause(&block)
e = self
while e
yield e
e = e.cause
end
end
end
@sugamasao
sugamasao / sample.js
Created January 17, 2018 15:59
javascriptでサロゲートペアを考慮したループとcode pointの取得
for(let c of 'い𩸽あ😇'){
console.log(c.charCodeAt(0).toString(16), c.charCodeAt(1).toString(16))
}
3044 NaN
d867 de3d
3042 NaN
d83d de07
@sugamasao
sugamasao / sample.sh
Created November 5, 2017 09:57
nokogiriをビルドしたい
# brew にあるのとXcodeとどっちをリンクしておくのが良いのだろうか(Xcodeの方がsymlinkなので汎用性ありそう)
% ls -l /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/
total 0
drwxr-xr-x 5 root wheel 160 11 3 12:39 MacOSX.sdk
lrwxr-xr-x 1 root wheel 10 9 20 22:16 MacOSX10.13.sdk -> MacOSX.sdk
% bundle config build.nokogiri --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2
@sugamasao
sugamasao / exception.rb
Created October 20, 2017 09:18
Exception#cause
=begin
https://docs.ruby-lang.org/ja/latest/class/Exception.html#I_CAUSE
[START] -> foo error.
[FooError-0] : foo error. -> ["exception_sample.rb:19:in `rescue in run'", "exception_sample.rb:16:in `run'", "exception_sample.rb:37:in `<main>'"]
[BarError-1] : bar error. -> ["exception_sample.rb:9:in `rescue in run'", "exception_sample.rb:6:in `run'", "exception_sample.rb:17:in `run'", "exception_sample.rb:37:in `<main>'"]
[ZeroDivisionError-2] : divided by 0 -> ["exception_sample.rb:7:in `/'", "exception_sample.rb:7:in `run'", "exception_sample.rb:17:in `run'", "exception_sample.rb:37:in `<main>'"]
[END]
=end
irb(main):001:0> begin
irb(main):002:1* raise ArgumentError, '何かエラー'
irb(main):003:1> rescue => e
irb(main):004:1> puts "e.class = #{ e.class }"
irb(main):005:1> puts "e.message = #{ e.message }"
irb(main):006:1> end
e.class = ArgumentError
e.message = 何かエラー
=> nil