Skip to content

Instantly share code, notes, and snippets.

View okuramasafumi's full-sized avatar
:octocat:
Hire me!

OKURA Masafumi okuramasafumi

:octocat:
Hire me!
View GitHub Profile
@okuramasafumi
okuramasafumi / define_mixable_method.rb
Created March 25, 2022 16:16
Mixable method where we can sort method call sequence
class Module
def define_mixable_method(name:, calls:, main:)
define_method(name) do |&block|
method_candidates = calls.map {|name| method(name) }
block.call(main, method_candidates).each do |m|
m.call
end
end
end
end
@okuramasafumi
okuramasafumi / each_with_effect.rb
Created March 22, 2022 13:42
EachWithEffect module for Ruby
module EachWithEffect
DEFAULT = Object.new.freeze
def each_with_effect(effect_handlers = {}, &block)
effect = catch(:effect) do
block.call(self.peek)
end
if effect
handler = effect_handlers.find{ |k, v| k === effect }&.last || effect_handlers.fetch(DEFAULT)
alternative = handler.call(effect) if handler
@okuramasafumi
okuramasafumi / test.rb
Created March 19, 2022 14:17
Algebraic Effects with Ruby
class WithEffect
def initialize
@list = ["Foo", "Bar", 42, "Baz"]
end
def process_item(item)
throw(:item_not_string, 42) unless item.is_a?(String)
puts item
end
@okuramasafumi
okuramasafumi / fzf-git.zsh
Last active January 18, 2024 14:32 — forked from junegunn/functions.sh
Key bindings for git with fzf (https://junegunn.kr/2016/07/fzf-git/)
# GIT heart FZF
# -------------
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
fzf-down() {
fzf --height 50% --min-height 20 --border --bind ctrl-/:toggle-preview "$@"
}
@okuramasafumi
okuramasafumi / mydsl.rb
Last active February 16, 2021 12:55
MyDSL
# ライブラリコード
module MyDSL
class ClassMacro < Module
class StoredItem
attr_reader :name
attr_accessor :context
def initialize(name, block_args: [], &block)
@name = name
@block_args = block_args
@block = block
@okuramasafumi
okuramasafumi / bench.rb
Created November 12, 2020 07:08
Ruby sum benchmark
require 'benchmark'
huge_array = (1..10000000).to_a
Benchmark.bmbm do |x|
puts "Adding all numbers"
x.report(:sum) { huge_array.sum }
x.report(:plus_equal) { sum = 0; huge_array.each {|i| sum += i }; sum}
x.report(:inject) { huge_array.inject(:+) }
x.report(:inject_block) { huge_array.inject(0) {|memo, i| memo + i} }
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "rails"
gem "sqlite3"
gem "jbuilder"
gem "active_model_serializers"
@okuramasafumi
okuramasafumi / after.rb
Created May 18, 2020 12:54
ActiveModelSerializerのこう書きたい
class HogeSerializer < ActiveModel::Serializer
attributes :id, :hogehoge
has_many :fugas do |fuga|
fuga.attributes :id, :fugafuga
fuga.has_many :bars do |bar|
bar.attributes :id, :barbar
end
end
end
Keynote: Vim Renaissance
By Prabir Shrestha
"I'm thankful to people in vim-jp"
"With LSP, you don't have to configure for every language!"
"If you're not using LSP, go use it!"
It works with every language, every OS, in Vim8 or Neovim...Great!
He talks about vim so passionately, that's awesome.
He says "Go use it!" so many times, I'd like to use LSP more!
diff --git a/proc.c b/proc.c
index 19ce7a1d19..3eb8c9445d 100644
--- a/proc.c
+++ b/proc.c
@@ -1196,6 +1196,24 @@ iseq_location(const rb_iseq_t *iseq)
return rb_ary_new4(2, loc);
}
+static VALUE
+iseq_code_location(const rb_iseq_t *iseq)