Skip to content

Instantly share code, notes, and snippets.

View kyuden's full-sized avatar

Masahiro Kyuden kyuden

View GitHub Profile
@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
@kyuden
kyuden / enumberalbe
Last active August 29, 2015 14:04
map vs inject vs each_with_object
# map(collect)
### 特徴
レシーバの配列の要素、1つずつに対して処理を行う
一つずつというのが重要で、処理前と処理後の配列のsizeは結局同じ
### 使い所
配列→配列の場合に使う
配列内容の要素すべてを1つずつ同様の処理を行うのに便利
例えば
# []があるから範囲指定の文字列抽出用途では価値ない
"2012-08-01".slice(0..2)
"2012-08-01"[0..2]
# 正規表現で抽出文字列を絞り込めるはおおきな利点
"2012-08-01".slice(/-(.+)-/) # => "-08-"
# ちなみにdeleteは削除後の文字列を返却するのが違い
"2012-08-01".delete(/-(.+)-/) # => "201201"
# 02
message = "hello"
if message.length > 1
alert message
# 03
s = "hello
safdaf
asdf
asfdfda"
@kyuden
kyuden / collection
Last active August 29, 2015 14:02
collection
numbers = [1, 2, 3, 4, 5, 6]
result1 = []
numbers.each do |num|
result1 << num * 2 if num % 2 == 0
end
p result1 # => [4, 8, 12]
@kyuden
kyuden / gist:9327279
Created March 3, 2014 15:25
Guardfile
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'livereload' do
watch(%r{app/views/.+\.(erb|haml|slim)$})
watch(%r{app/helpers/.+\.rb})
watch(%r{public/.+\.(css|js|html)})
watch(%r{config/locales/.+\.yml})
# Rails Assets Pipeline
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
@kyuden
kyuden / file0.txt
Created February 27, 2014 16:39
guard-rspecでRSpecのバージョン不一致によるエラー対処法 ref: http://qiita.com/Kyuden@github/items/ef597250d673a2608e49
bundle exec guard
01:12:33 - INFO - Guard is using TerminalTitle to send notifications.
01:12:33 - INFO - Guard::RSpec is running
01:12:33 - INFO - Guard is now watching at '/Users/kyuden/Test'
[1] guard(main)>
01:12:34 - INFO - Run all
01:12:34 - INFO - Running all specs
WARN: Unresolved specs during Gem::Specification.reset:
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
@kyuden
kyuden / describe vs. context in rspec
Created February 8, 2014 20:27
describe vs. context in rspec
describe vs. context in rspec
http://lmws.net/describe-vs-context-in-rspec
In Rspec world, you often see people using both “describe” blocks and “context” blocks together, like this
describe "launch the rocket" do
context "all ready" do
end
context "not ready" do
@kyuden
kyuden / gist:8888098
Created February 8, 2014 18:40
how to clone remote_branch on local
# display remote_branch
gir branch -r
# clone remote_branch on local
git checkout 'branch_name' origin/'branch_name'