Skip to content

Instantly share code, notes, and snippets.

// useReducer
// https://ja.reactjs.org/docs/hooks-reference.html#usereducer
// basic
// const [state, dispatch] = useReducer(reducer, initialArg, init);
// reducer type
// (state, action) => newState;
// when it use??
@SyunWatanabe
SyunWatanabe / showmeta.js
Last active January 25, 2021 03:27
show meta
// ==UserScript==
// @name show meta
// @namespace http://tampermonkey.net/
// @version 0.1
// @description show meta what we want
// @author me
// @match *://*/*
// @grant none
// ==/UserScript==
# Ruby Document
# https://docs.ruby-lang.org/ja/latest/class/Enumerable.html#I_PARTITION
# github: holiday_japan
# https://github.com/masa16/holiday_japan/blob/master/lib/holiday_japan.rb
require 'holiday_japan'
# Enumerable#partitionを使い2020年の祝日がある月を抽出する
# frozen_string_literal: true
# block
# simple
def say_fact
if block_given?
puts 'i got block'
else
puts 'i need block'
# count 再実装
class Array
def count(*args)
if block_given?
num = 0
each { |i| num += 1 if yield(i) }
num
elsif args.empty?
size
else
# 例外処理 サンプルそのまま
print "Text?:"
text = gets.chomp
begin
print "Pattern?:"
pattern = gets.chomp
regexp = Regexp.new(pattern)
rescue RegexpError => e
puts "Invalid regexp #{e.message}"
retry
# Moduleを使う DeepFreeze(自作)
module Freeze
def deep_freeze(object)
object.freeze
object.each do |element, value|
element.freeze
value&.freeze
end
end
end
# 改札機プログラム(自力作)
STATION = ['harajyuku', 'omotesando', 'nogizaka']
class Gate
attr_accessor :name
def initialize(name)
@name = name
end
# 正規表現変換
old_syntax = <<TEXT
{
:name => 'Alice',
:age=>20,
:gender => :female
}
TEXT
# 長さの単位変換プログラム
def convert_length(num, unit, conversion)
hash = {'m' => 1.00, 'ft' => 3.28, 'in' => 39.37}
(num * hash[conversion] / hash[unit]).round(2)
end
puts convert_length(1, 'm', 'in')
puts convert_length(15, 'in', 'm')
puts convert_length(35_000, 'ft', 'm')