開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Ruby 2.3 (プログラミング言語)
Head First Ruby (Jay McGavren (著)、O'Reilly Media)のChapter 6.(Block Return Values: How Should I Handl This?)、Opening the file、Safely closing the file, with a block、Don't forget about variable scope!、EXERCISE(No. 3516)を取り組んでみる。
EXERCISE(No. 3516)
コード(Emacs)
#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*-
def yield_number
yield 4
end
array = [1, 2, 3]
yield_number { |number| array << number }
p array
sum = 0
[1, 2, 3].each { |number| sum += number}
puts sum
contents = []
File.open("sample.txt") do |file|
contents = file.readlines
end
puts contents
入出力結果(Terminal)
$ ./sample1.rb [1, 2, 3, 4] 6 This is the first line in the file. This is the second. this is the last line. $
0 コメント:
コメントを投稿