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
# 属性や状態の取得 | |
fname = '/etc/hosts' # Windowsでは 'C:¥Windows¥System32¥drivers¥etc¥hosts' | |
File.atime(fname) # 最終アクセス日時 => 2018-01-24 19:28:45 +0900 | |
irb(main):004:0> File.ctime(fname) # 最後に状態を変更した日時 => 2017-12-09 12:23:36 +0900 | |
irb(main):005:0> File.mtime(fname) # 最終更新日時 => 2017-12-04 19:03:18 +0900 | |
# ファイルの存在 | |
File.exists?(fname) # => true |
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
# ファイル名の変更 | |
File.renaem 'src', 'dest' | |
# ファイルの移動 | |
File.rename 'src', 'dir/dest' | |
# ファイルの削除 | |
File.unlink 'fname' | |
# シンボリックリンク |
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
# ファイルのロック | |
File.open 'counter', File::RDWR | File::CREAT do |f| | |
f.flock File::LOCK_EX | |
count = f.read.to_i | |
f.rewind | |
f.write count.succ | |
end | |
# ファイルに関する情報の取得 | |
File.open 'counter' do |f| |
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
# ファイルを読み込む | |
file = File.open('fname.txt') | |
puts file.read | |
file.close | |
# ブロックで記述するとすっきり書ける | |
File.open 'fname.txt' do |file| | |
puts file.read | |
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
Time.now # => 2017-09-27 19:59:10 +0900 | |
Time.new # => 2017-09-27 19:59:10 +0900 | |
# タイムゾーンを得る | |
Time.now.zone # => "JST" | |
# UTC時間を得る | |
Time.now.getutc # => 2017-09-27 11:00:01 UTC | |
# タイムゾーンをUTCに変更する |
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
# Ruby2.0からEnumerator::Lazyが追加された | |
(0..Float::INFINITY).map { |n| n.succ }.select {|n| n.odd?}.take(3) # 返ってこない | |
# lazyによって必要な分だけ計算し結果を得ることができる | |
odd_numbers = (0..Float::INFINITY).lazy.map { |n| n.succ }.select {|n| n.odd?}.take(3) | |
odd_numbers.force # => [1, 3, 5] | |
# lazyを用いた場合は要素を繰り返す度にmapとselectが実行されていることがわかる | |
(0..Float::INFINITY).lazy.map { |n| | |
puts "map: #{n}" |
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
setInterval(function() { | |
img = document.createElement("img"); | |
img.src = "https://qiita-cloud.s3-ap-northeast-1.amazonaws.com/emoji/innocent.png"; | |
document.body.appendChild(img); | |
document.body.insertBefore(img, document.body.firstChild); | |
}, 500); |
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
# 様々なクラスがEnumeratorを返すメソッドを持っている | |
[].each | |
{}.each | |
(1..10).each | |
''.each_char | |
10.times | |
# loop | |
#to_enum/enum_forメソッドでも得られる | |
[1,2,3].to_enum |
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
# マージ | |
a = { one: 'A', two: nil } | |
b = { two: 'B', three: 'C' } | |
a.merge(b) # => {:one=>"A", :two=>"B", :three=>"C"} | |
# mergeは疑似キーワード引数にデフォルト値をもたせたいときに便利 | |
def keywords(hash = {}) | |
defaults = {alice: 'ありす', bob: 'ぼぶ'} # デフォルト値のハッシュ | |
hash = defaults.merge(hash) | |
hash |
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
# Hash | |
array = [1, 2] | |
# Hashのキーには通常、Object#hashの値が使われる | |
# ミュータブルなオブジェクトはHashのキーに向かない | |
array.hash # => -2511718100518778594 | |
array.reverse! # => [2, 1] | |
array.hash # => -206156502938470765 | |
# Stringもミュータブルだが、利便性のために例外扱いとなっている |
NewerOlder