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 / rdoc-sequence-diagram.mmd
Last active December 17, 2024 18:47
RDoc diagrams, generated by Copilot
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@okuramasafumi
okuramasafumi / main.rb
Last active December 14, 2024 19:13
Rubyists OPMLの作り方
require 'builder'
require 'json'
list = JSON.load_file('list.json')
builder = Builder::XmlMarkup.new
builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml = builder.opml(version: '1.0') do |opml|
opml.head do |head|
head.title("This is the title")
head.dateCreated(Time.now)
@okuramasafumi
okuramasafumi / list.txt
Created August 20, 2024 17:42
List of Programmers, from Wikipedia article
"Michael Abrash",
"Scott Adams (game designer)",
"Tarn Adams",
"Leonard Adleman",
"Alfred Aho",
"Andrei Alexandrescu",
"Paul Allen",
"Eric Allman",
"Marc Andreessen",
"Jeremy Ashkenas",
@okuramasafumi
okuramasafumi / main.rb
Created May 30, 2023 08:32
RSpec documentation without executing
require 'syntax_tree'
class RSpecEnvironment
def initialize
@contexts = []
@results = []
end
def klass(k)
@contexts << k
@okuramasafumi
okuramasafumi / string_scanner.rb
Created May 27, 2023 16:33
Pure Ruby implementation of StringScanner
class MyStringScanner
class ScanError < StandardError; end
attr_reader :string, :captures
attr_accessor :pos
def initialize(string, _obsolete = false, fixed_anchor: false)
@string = string
@fixed_anchor = fixed_anchor
@pos = 0
@previous_pos = 0
@okuramasafumi
okuramasafumi / included.rb
Created April 17, 2023 13:27
Tell me if this module is included by the same class more than once
def self.included(base)
base.instance_variable_set(:@count, 0) unless base.instance_variable_defined?(:@count)
base.instance_variable_set(:@count, base.instance_variable_get(:@count) + 1)
puts base.name if base.instance_variable_get(:@count) > 1
end
@okuramasafumi
okuramasafumi / hash.rb
Last active April 2, 2023 11:56
[Proposal] Hash#transform in Ruby
class Hash
def transform(&block)
new_hash = {}
each_pair do |key, value|
rtn = block.call(key, value)
case rtn
in nil | true then new_hash[key] = value
in false then next
in Array[[_, _], *]
rtn.each do |k, v|
@okuramasafumi
okuramasafumi / hash_vs_case.rb
Created August 11, 2022 10:01
Performance comparison between hash lookup and case lookup
MAPPING = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
}.freeze
def method1(key)
@okuramasafumi
okuramasafumi / ice.rb
Created March 26, 2022 15:54
Ice feature from zinit in Ruby
module Kernel
def ice(key, value, on: Object)
if on.instance_methods.include?(key.to_sym)
prev = on.instance_method(key)
on.define_method key do
on.remove_method key
on.define_method(key, prev)
return value
end
else
@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