アウトプットができる技術者に

it's a time to take a new step !

2013-02-26から1日間の記事一覧

ファイル書き込み

open("tmp.txt","w"){|f| f.puts("a") f.write("b") } puts だと 改行あり write だと 改行なし reference http://doc.ruby-lang.org/ja/1.9.3/class/File.html

配列

arr = [1,2,3] arr << 4 p (arr[0]) # => 1 p (arr[3]) # => 4 http://doc.ruby-lang.org/ja/1.9.2/class/Array.html

置換 gsub

s = "a,b" p s.gsub(",",":") # => "a:b" 正規表現置換 s = "a,b,b" p s.gsub(/b$/,"x") # => "a,b,x"

Regex

s="abc" if s =~ /^a+/ then p s # => abc end 否定 s="abc" if s !~ /^a+/ then p s end

split

str = "a,b,c" p str.split(",") # => ["a", "b", "c"]

for文

for i in 1..5 do p i end

正規表現 パターンマッチング

/a(.)c(.)e(.)/ =~ "abcdef" p $1 #=> b p $2 #=> d p $3 #=> f