Skip to content

Instantly share code, notes, and snippets.

@sugamasao
sugamasao / sample.rb
Last active December 12, 2022 07:13
Structに読み取り専用のmemberがほしい.rb
Person = Struct.new(:name, :age, :hoge, keyword_init: true) do
def initialize(**kw)
raise 'hoge は指定できへんで' if kw[:hoge]
super
self.hoge = :xxx
undef :hoge=
end
end
person = Person.new(name: 'foo', age: 10)
@sugamasao
sugamasao / client.rb
Created September 10, 2022 07:36
dRubyで遊んでみよう
require 'drb/drb'
require 'logger'
require_relative './user'
URL = 'druby://localhost:8787'
server = DRbObject.new_with_uri(URL)
logger = Logger.new(STDOUT)
3.times do
logger.info "call server"
@sugamasao
sugamasao / bench.rb
Created September 18, 2020 08:49
delete suffix
require 'benchmark/ips'
TIME = 10_00
STR = "ruby"
puts RUBY_VERSION
Benchmark.ips do |bm|
bm.report "gsub" do
TIME.times do
@sugamasao
sugamasao / RubyKaigi-2020-takeout.md
Last active September 1, 2020 01:56
RubyKaigi 2020-takeout
Person = Struct.new(:name, :age, keyword_init: true) do
def greet
puts "#{name} : #{age}"
end
end
person = Person.new(name: 'taro', age: 20)
person.greet #=> taro : 20
@sugamasao
sugamasao / sample.rb
Last active June 27, 2020 04:25
action mailerで場合によってtextだけにしたい&フォーマットによってテンプレートパスを変えたい場合
mail(param) do |format|
format.html { render 'user_mailer/welcome' } unless @text_dake_okuritai
format.text { render 'user_mailer/text/welcome' } # path/to/name
end
@sugamasao
sugamasao / .gitconfig
Last active April 23, 2020 03:42
git browse foo/bar.txt 10 でGitHubのリポジトリ開くくん
[alias]
browse = !"f() { open $(git remote get-url origin)/tree/$(git rev-parse HEAD)/${1}#L${2}; }; f"
@sugamasao
sugamasao / sample.rb
Created April 3, 2020 14:18
Ruby 2.7のhashの警告動作
def bar(h, **kwargs)
p h, kwargs
end
# warning
bar(k: 42)
bar({k: 42}, {k2: 10})
# ok
bar({k: 42})
@sugamasao
sugamasao / foo.rb
Created January 9, 2020 00:35
attr_reader
class Foo
attr_reader :bar
def initialize(bar)
@bar = bar
end
def hoge
puts "bar is #{bar}"
end
end
@sugamasao
sugamasao / main.go
Created July 2, 2019 14:23
perlを実行するやーつのruby版
package main
import (
"fmt"
"io/ioutil"
)
const script = `
#!ruby
puts "Hello, Ruby! VERSION = #{RUBY_VERSION}"