Skip to content

Instantly share code, notes, and snippets.

@ganmacs
ganmacs / gist:1ceafc33555e05afe743
Last active August 29, 2015 14:01
Rubyのuniqの条件を複数指定する
hash = [{id: 1, name: 'taro', old: 22, sex: 'men'},
{id: 2, name: 'hanako', old: 22, sex: 'woman'},
{id: 3, name: 'taro', old: 24, sex: 'men'},
{id: 4, name: 'taro', old: 24, sex: 'woman'},
{id: 5, name: 'taro', old: 22, sex: 'woman'},
]
p hash.uniq { |h| [h[:name], h[:old]] }
# => [{:id=>1, :name=>"taro", :old=>22, :sex=>"men"}, {:id=>2, :pname=>"hanako", :old=>22, :sex=>"woman"}, {:id=>3, :name=>"taro", :old=>24, :sex=>"men"}]
@ganmacs
ganmacs / gist:e08f1420909f867438ba
Created July 3, 2014 13:52
最小値の添字を取得
ary = [1, 10, 2, 5, 8, 4, 6]
ary.each_with_index.max[1]
@ganmacs
ganmacs / gist:949b6f34d5613005da10
Created July 8, 2014 14:02
Hash#sortの返り値がArrayなのでどうにかする
a = {c: 3, b: 2, dd: 5, a: 1}
p a.sort.to_h
# メソッド内で
value = foo_method
return if value.nil?
# 下でも一緒
value = foo_method.tap { |e| return if e.nil?}
@ganmacs
ganmacs / gist:a498999f33b91a34dd15
Last active August 29, 2015 14:18
rubocop.yml
AllCops:
Exclude:
- 'db/schema.rb'
- 'vendor/**/*'
- 'db/migrate/**/*'
Metrics/LineLength:
Max: 120
Metrics/CyclomaticComplexity:
@ganmacs
ganmacs / gist:097353b76befb74e9566
Last active August 29, 2015 14:20
forwadable tekina
require 'forwardable'
class A
extend Forwardable
def_delegators :@ary, :size, :[]=, :[], :<<, :to_s, :inspect
def initialize
@ary = []
end
end
@ganmacs
ganmacs / gist:e71d96e97e457239bec1
Last active August 29, 2015 14:21
tree っぽい何か
find . -type f | grep -v git | awk -F/ '{t=" |---"}n==$2{printf "%s%s\n",t,$3}n!=$2&&$3!=""{n=$2;printf "%s \n%s%s\n",$2,t,$3}n!=$2&&$3==""{print $2}' | awk '/^ /{printf "| %s\n", $0} /^[^ ]/{printf "|--%s\n", $1}' | gtac | awk 'BEGIN{n=0} n==1{print $0}n==0{printf " %s\n",$2}n==0&&$2==""{print}$2==""{n=1}END{print "."}' | gtac | grep -v ' $'
module A
class << self
def setup!
@config = 'config'
end
def setup
@config
end
class A
CONFIGURES = %i(user mail pass).freeze
def initialize(config = {})
CONFIGURES.each do |e|
instance_variable_set(:"@#{e}", config[e] || 'nil')
end
end
end
@ganmacs
ganmacs / gist:4a8243575beb68d03a20
Last active August 29, 2015 14:22
Return the method name which is in running now
class Object
def current_method_name
caller.first.match(/`(?<method_name>.*)'/)[:method_name]
end
end
puts current_method_name #=> "<main>"