Skip to content

Instantly share code, notes, and snippets.

View JunichiIto's full-sized avatar

Junichi Ito JunichiIto

View GitHub Profile
require 'benchmark'
def with_sum
(0..9_999_999)
.map { |n| n * 2 }
.select { |n| n % 3 == 0 }
.sum
end
def with_inject
@JunichiIto
JunichiIto / sample_spec.rb
Created June 15, 2017 01:43
Check exception presence in after hook
RSpec.describe 'Sample' do
after do |example|
if example.exception
puts 'Error!'
else
puts 'OK!'
end
end
example 'sample' do
@JunichiIto
JunichiIto / qiita-update-ranking.rb
Created October 16, 2017 00:25
Analyze the update ranking of "随時更新" items.
require 'minitest/autorun'
require 'json'
require 'date'
def execute
# How to create qiita.json
# 1. curl -o qiita.json https://qiita.com/api/v2/items?page=1&per_page=100&query=title%3A%E9%9A%8F%E6%99%82%E6%9B%B4%E6%96%B0
# 2. curl -o qiita.json https://qiita.com/api/v2/items?page=2&per_page=100&query=title%3A%E9%9A%8F%E6%99%82%E6%9B%B4%E6%96%B0
# 3. Merge them manually
path = '/Users/jit/Desktop/qiita.json'
@JunichiIto
JunichiIto / benchmark.rb
Created December 14, 2017 04:39
How slow Ruby exception is?
# Based on http://blog.honeybadger.io/benchmarking-exceptions-in-ruby-yep-theyre-slow/
require 'benchmark/ips'
def find_by
nil
end
def find_by!
raise 'Not found'
end
# example(it)内でローカル変数を宣言する
describe 'convert_hash_syntax' do
it '=>が:に置き換わる' do
old_syntax = <<~TEXT
{
:name => 'Alice',
:age => 20,
:gender => :female
}
TEXT
@JunichiIto
JunichiIto / answer-2018.03.29.md
Last active March 29, 2018 12:36
Question and answer about プロを目指す人のためのRuby入門

Q.

p250で、クラスメソッドをprivateにしたい場合とありますが、privateメソッドは「レシーバを指定して呼び出すことができない」とあるので不思議です。 User.hello のUserはレシーバとは呼ばないのかということです。

https://twitter.com/maehrm/status/979326471075278848

A.

いいえ、Userはレシーバです。なので、クラスメソッドのhelloがprivateメソッドだった場合は、User.helloのようにレシーバを指定して呼び出すとエラーになります。

  1. RubyMineを終了する
  2. 対象プロジェクトのディレクトリをリネームする(例: mv awesome-project awesome-project.old
  3. GitHubからプロジェクトを再度ローカルにcloneする
  4. コマンドラインからbundle installする
  5. RubyMineを起動する
  6. 上記3でcloneしたプロジェクトを開く

こちらの検証結果

irbで実行したところ、本書の記述通りになりました(Mac、Windowsとも)

screen shot 2018-07-27 at 3 51 01

本書のサンプルコードは特に断りがない限り、irb上での出力結果を載せています(1.6.1項参照)。

ファイルに保存して出力した場合

なんとなくの予想なのですが、ぬうさんは次のようなコードをファイルに保存して実行したのではないでしょうか?

@JunichiIto
JunichiIto / dvd.applescript
Last active October 21, 2018 01:32
Use VLC to open VIDEO_TS folder instead of DVD Player app for macOS Mojave
tell application "VLC"
activate
open file "Users:your-name:Documents:some-dvd:VIDEO_TS"
play
end tell
@JunichiIto
JunichiIto / regex.txt
Last active October 28, 2018 01:15
Regex test for ^ and $.
$ irb
irb(main):001:0> "".match? /^/
=> true
irb(main):002:0> "".match? /$/
=> true
irb(main):003:0> "".match? /^$/
=> true
irb(main):017:0> code = <<RUBY
irb(main):018:0" [1, 2, 3].each do