Skip to content

Instantly share code, notes, and snippets.

Avatar

Yudai Tanaka ytnk531

View GitHub Profile
View proxy_pattern.rb
# ItemProxyはItemImplと同じくprice()とprint_self()を公開する。rubyは共 通のインターフェースを強制するようなスタイルのコーディングはしないので 特に何も書かない。
class ItemImpl
attr_reader :price
def initialize(price)
@price = price
end
def print_self
puts "This item is #{price} yen."
@ytnk531
ytnk531 / parallel_bench.rb
Created Jan 31, 2021
Ruby parallel execution benchmark.
View parallel_bench.rb
PARALLELISM = 16
def solve
(1..5_000_000).inject(:+)
end
def solve_ractor
ractors = PARALLELISM.times.map { Ractor.new { solve } }
ractors.each { Ractor.select _1 }
end
@ytnk531
ytnk531 / transaction_exception.rb
Created Jan 27, 2021
Rollback will not occur situation in RubyOnRails
View transaction_exception.rb
require 'rails_helper'
def exec_transaction
ApplicationRecord.transaction do
User.create!(name: 'Duplicate')
User.create!(name: 'Duplicate')
end
end
def exec_transaction_rescue
@ytnk531
ytnk531 / setup.sh
Created Jan 24, 2021
My ruby development environment build log.
View setup.sh
#bin/sh
ln -sf ~/dotfiles/.zshrc ~/.zshrc
ln -sf ~/dotfiles/.vimrc ~/.vimrc
ln -sf ~/dotfiles/.vimrc ~/.config/nvim/init.vim
ln -sf ~/dotfiles/.tmux.conf ~/.tmux.conf
#zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma/zinit/master/doc/install.sh)"
# nvim
@ytnk531
ytnk531 / install-idea.sh
Last active Jan 24, 2021
Install IntellijIDEA, RubyMine and CLion on commandline. After this installation, idea, rubymine and clion command will be available. Ubuntu 20.04.1 LTS
View install-idea.sh
curl -LO https://download.jetbrains.com/idea/ideaIU-2020.3.1.tar.gz
sudo tar xzf ideaIU-2020.3.1.tar.gz -C /usr/local/share
sudo ln -s /usr/local/share/idea-IU-203.6682.168/bin/idea.sh /usr/local/bin/idea
curl -LO https://download.jetbrains.com/ruby/RubyMine-2020.3.1.tar.gz
sudo tar xzf RubyMine-2020.3.1.tar.gz -C /usr/local/share
sudo ln -s /usr/local/share/RubyMine-2020.3.1/bin/rubymine.sh /usr/local/bin/rubymine
curl -LO https://download.jetbrains.com/cpp/CLion-2020.3.1.tar.gz
sudo tar xzf CLion-2020.3.1.tar.gz -C /usr/local/share
@ytnk531
ytnk531 / setup-ruby.ps1
Created Jan 22, 2021
Setup mri ruby development environment on Windows 10.
View setup-ruby.ps1
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
scoop install git ruby bison m4 sed patch gzip
git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
.\vcpkg\vcpkg --triplet x64-windows install openssl zlib
win32\configure.bat --without-ext=+,dbm,gdbm,readline --enable-bundled-libffi --with-opt-dir=C:/opt/local/src/github.com/Microsoft/vcpkg/installed/x64-windows
@ytnk531
ytnk531 / nokogiri_ex.rb
Created Jan 2, 2021
parse image on google search
View nokogiri_ex.rb
require 'uri'
require 'open-uri'
require 'net/http'
require 'bundler/inline'
require 'securerandom'
gemfile do
source 'https://rubygems.org'
gem 'nokogiri'
end
View fiddle_ex.rb
require 'fiddle'
libc = Fiddle.dlopen('libc.so.6')
printf = Fiddle::Function.new(libc['printf'],
[Fiddle::TYPE_VOIDP],
Fiddle::TYPE_INT
)
printf.call "Hello with Fiddle::Function\n"
require 'fiddle/import'
View gilded_rose.rb
class GildedRose
NAME_SULFURAS = 'Sulfuras, Hand of Ragnaros'.freeze
NAME_BRIE = 'Aged Brie'.freeze
NAME_PASS = 'Backstage passes to a TAFKAL80ETC concert'.freeze
MAX_QUALITY = 50
def initialize(items)
@items = items
end
View fiber_schedule.rb
require 'nio'
require 'fiber'
require 'timers'
require 'io/nonblock'
class Scheduler
def initialize
@selector = NIO::Selector.new
@fibers = {}
@timers = Timers::Group.new