Skip to content

Instantly share code, notes, and snippets.

View kyuden's full-sized avatar

Masahiro Kyuden kyuden

View GitHub Profile
class ProducerConsumer
KILL = :kill
def initialize(produce:, consume:, producer_num: 1, consumer_num: 5, max_queue_size: 1000)
@produce = produce
@consume = consume
@consumer_num = consumer_num
@producer_num = producer_num
@ids_index = 0
@max_queue_size = max_queue_size
# バックナンバー
### Rubyわくわくナビ
- 2012-66:Bundlerを使いこなす!
- 2012-67:厳選!用途別Ruby/Railsライブラリツール
- 2012-68:データで見るRubyGemsの世界
### 一歩先をゆくRuby
@kyuden
kyuden / typeof.js
Last active October 16, 2015 10:00
社内JS勉強会 : typeof検定
// 環境 node v4.1.2
// 実行せず出力結果を記述しなさい
console.log(typeof 'foo');
console.log(typeof new Object());
console.log(typeof undefined);
console.log(typeof new Function('x', 'y', 'return x * y'));
console.log(typeof true);
console.log(typeof /abc/g);
console.log(typeof String('hoge'));
console.log(typeof 10);
@kyuden
kyuden / unread.md
Created February 15, 2015 11:32
unread 概略構成
@kyuden
kyuden / cattr_accessor_vs_class_attribute
Last active August 29, 2015 14:15
cattr_accessor vs class_attribute
[1] pry(main)> class Base
[1] pry(main)* cattr_accessor :setting2
[1] pry(main)* end
=> [:setting2]
[2] pry(main)> class Subclass < Base ; end
=> nil
[3] pry(main)> Base.setting2
=> nil
[4] pry(main)> Base.setting2 = true
@kyuden
kyuden / gist:654236a100f81d03d22b
Created February 1, 2015 13:57
nilチェック入りeach
(User.hoges || []).each do |hoge|
hoge.fuga!
end
@kyuden
kyuden / mutable_constant.rb
Last active August 29, 2015 14:13
定数フリーズまとめ
class MutableConstant
SERVICE_TYPES1 = ["basic", "premium", "pro"]
SERVICE_TYPES2 = ["basic", "premium", "pro"].freeze
SERVICE_TYPES3 = ["basic", "premium", "pro"].map(&:freeze).freeze
module Defaults
SERVICE_TYPES1 = ["basic", "premium", "pro"]
SERVICE_TYPES2 = ["basic", "premium", "pro"].freeze
SERVICE_TYPES3 = ["basic", "premium", "pro"].map(&:freeze).freeze
end
#!/usr/bin/env ruby
require "webrick"
=begin
WEBrick is a Ruby library that makes it easy to build an HTTP server with Ruby.
It comes with most installations of Ruby by default (it’s part of the standard library),
so you can usually create a basic web/HTTP server with only several lines of code.
The following code creates a generic WEBrick server on the local machine on port 1234,
@kyuden
kyuden / gist:5f56bc14efe5f56b0979
Created August 4, 2014 01:22
Enumable条件検索系
Enumable#grep
レシーバと引数が===と一致するかどうかで絞り込む
# ===で一致するか、ということは==の条件に「範囲ないか」「インスタンス、クラスに属するか」「正規表現に一致するか」は加わったもの
(1..10).grep(2..3) #=> [2, 3]
%w|a 1 * @|.grep(/[1-9]/) #=> ["1"]
["a", 1, "&", "@"].grep(Integer) #=> [1]
grepの良い所は条件でしぼりこんだあとにブロックを渡して更に処理ができる
@kyuden
kyuden / gist:fabce1d756ecd5e7c651
Last active August 29, 2015 14:04
Array#select(Enumerable#select), Hash#select
Hash#find_all
Hash#select
h = {a: 1, b: 2, c: 3}
h.find_all { |key, value| value.odd? } #=> [[:a, 1], [:c, 3]]
h.select { |key, value| value.odd? } #=> {:a=>1, :c=>3}
Array#select
Array#select