This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ライブラリコード | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 "$@" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MAPPING = { | |
| a: 1, | |
| b: 2, | |
| c: 3, | |
| d: 4, | |
| e: 5, | |
| f: 6, | |
| }.freeze | |
| def method1(key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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| |