-
-
Save ainame/1614299 to your computer and use it in GitHub Desktop.
RubyでRubyっぽい書き方をしよう初級編
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
# 1. for, whileを使わずにeachを使う | |
array = [1,2,3,4,5] | |
array.each do |e| | |
print e | |
end | |
#=> 12345 | |
# テキストファイルを一気に読み込んで, | |
# 1行ごとにイテレート | |
textfile = File.open("a.txt").read | |
textfile.each_line do |line| | |
puts line | |
end | |
# 10回繰り返し(xには0〜9までが入る) | |
sum = 0 | |
10.times do|x| | |
sum += x | |
end | |
puts sum #=> 45 |
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
# 2. 処理の単位,リソースの寿命をブロック(do 〜 end)で表す | |
File.open("foo.txt","w") do |f| | |
f.puts "あげぽよ" | |
end | |
# 本来ならcloseまで書かなきゃならないが, | |
# endとともにfoo.txtはcloseされる | |
# つまり以下のコードと一緒です | |
f = File.open("foo.txt", "w") | |
f.puts "あげぽよ" | |
f.close | |
# 例えばlogディレクトリ以下のファイルを操作したいとき | |
Dir.chdir("log") do |path| | |
system "rm *.log" | |
end | |
# 本来ならルートのディレクトリに戻るためには, | |
# Dir.chdir("../")とかかなきゃならないが, | |
# 処理が終わった後はルートに戻ってる |
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
# 3. 引数をハッシュで渡す | |
# = を使うことでデフォルト引数を指定できます | |
def bar ( options = {} ) | |
puts options[:hoge] | |
puts options[:piyo] | |
end | |
def foo ( a, options = {} ) | |
# options[:fuga]が存在しないときに"fuga"が入る | |
fuga = options.delete(:fuga) || "fuga" | |
bar(options) | |
puts fuga | |
end | |
# Rubyのメソッド呼び出しの最後の引数にハッシュが書かれていたときは, | |
# {}を省略してかけます. | |
foo ( "a", :hoge => 1, :fuga => 2, :piyo => 3 ) | |
#=> 1 | |
#=> 3 | |
#=> 2 | |
# --- | |
# fooからbarの呼び出しのように,引数のハッシュををさらに別のメソッドに渡してやるときは, | |
# 要らない要素をdeleteメソッドで消してから渡してやるとクールです | |
# ただし,deleteは副作用で呼び出し元のハッシュを壊してしまうので注意 | |
# 最近はキーワード引数というものが使えます. | |
# 上の呼び出しを以下のようにかけます | |
foo( hoge: 1, fuga: 2, piyo: 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
# 4. メソッドチェーンを利用する | |
#1行でこんなことも出来る | |
:"Hello World".to_s.sub("World", "Ruby").tap{|str| puts str} | |
#=> Hello Ruby | |
#*簡単な解説 | |
# レシーバーにメッセージを投げるという考え方をするとわかりやすいと思います. | |
# | |
# :"Hello World" :レシーバー(Symbolクラス) | |
# | |
# ↑メッセージを投げる | |
# | |
# to_s :Symbol#to_sメソッドが呼ばれて,:"Hello World"を文字列に変換 | |
# (Stringクラスのインスタンスを返る) | |
# | |
# ↑メッセージを投げる | |
# | |
# sub :レシーバーがStringクラスになったので次はString#subが呼ばれて | |
# "World"を"Ruby"に置き換え | |
# (Stringクラスのインスタンスを返る) | |
# | |
# ↑メッセージを投げる | |
# | |
# tap :基底クラスで定義されているObject#tapメソッドが呼ばれる. | |
# オブジェクトを受け取ってそのまま返すメソッドなので,そのブロック中でputsで印字する | |
# (Stringクラスのインスタンスが返る) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment