This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 二分探索 | |
# Array#bsearch | |
array = [1,3,5,7,9] # ソート済みである必要がある | |
# 6より大きいものを探索 | |
array.bsearch {|n| n > 6} # => 7 | |
# 10より大きいものを探索 | |
array.bsearch {|n| n > 10} # => nil | |
# 要素の連結 | |
# Array#join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nilを取り除く | |
array = [false, nil, 0, '', []] | |
array.compact # => [false, 0, "", []] | |
array # [false, nil, 0, '', []] | |
# 破壊的 | |
array.compact! | |
array # [false, 0, '', []] | |
# 重複を取り除く |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Facility | |
attr_reader :name, :cost, :income, :range, :color | |
def initialize(name:, cost:, income:, color:, range:) | |
@name = name | |
@cost = cost | |
@income = income | |
@color = color | |
@range = Array(range) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [4, 4, 2, 3] | |
# 0番目に代入 | |
array[0] = 5 | |
array # => [5, 4, 2, 3] | |
# サイズを上回る添字を指定して代入した場合、その間の要素はnilになる | |
array[10] = 1 | |
array # => [5, 4, 2, 3, nil, nil, nil, nil, nil, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [4,4,2,3] | |
array[2] # => 2 # 添字で取得する | |
array[2, 2] # => [2, 3] # 要素の2番目から2つ | |
array[2, 4] # => [2, 3] # 要素の2番目から4つ(3つめ以降は存在しないので取得されない) | |
array.values_at(1) # => [4] | |
array.values_at(1, 3) # => [4, 3] # 4番目と3番目 | |
# 先頭の要素を取得する | |
array.first # => 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Array.newでも配列を生成できる。(第一引数: 長さ 第二引数: 要素) | |
Array.new(5, 1) # => [1, 1, 1, 1, 1] | |
# この場合、全ての要素は同じオブジェクトとなる | |
array = Array.new(3, 'native') | |
array[0].reverse! | |
array # => ['evitan', 'evitan', 'evitan'] | |
# ブロックをわたすとindexに対して要素を変化させられる | |
Array.new(3) { |index| index.succ } # => [1, 2, 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: $ slowlog -i instance-name -d 2017-02-14 >> slowquery.log | |
require 'date' | |
require 'optparse' | |
option = {} | |
OptionParser.new do |opt| | |
opt.on('-i VALUE', '--instance=VALUE', 'DB Instance ex. instance-name') {|v| option[:instance] = v} | |
opt.on('-d VALUE', '--date=VALUE', 'Default: today ex. 2017-02-07') {|v| option[:date] = Date.parse(v) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enumerable#sort, sort_by | |
people = %w(alice bob charlie) | |
# 要素間でソート | |
people.sort | |
# => ["alice", "bob", "charlie"] | |
# ブロックを評価した結果でソート | |
people.sort{|a, b| a.length <=> b.length } | |
# => ["bob", "alice", "charlie"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enumerable#max,min,minmax | |
range = (1..10) | |
# 最大値 | |
range.max # => 10 | |
# 最小値 | |
range.min # => 1 | |
# 最大値と最小値 | |
range.minmax # => [1, 10] | |
# Enumerable#max_by,min_by,minmax_by |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enumerable#group_by | |
# 型ごとにグルーピングする | |
array = [1, 2.0, 3.0, 4] | |
array.group_by{ |val| val.class } | |
# => {Integer=>[1, 4], Float=>[2.0, 3.0]} | |
# 奇数/偶数でグルーピングする | |
array.group_by{ |val| val.to_i.odd? } | |
# => {true=>[1, 3.0], false=>[2.0, 4]} |