print("love the world.\n")んで、ワンライナーで実行した結果がこれ。
C:\>ruby -e 'print "love the world.\n"' love the world. C:\>
変数に代入して表示してみる。
test1.rbl = "love" t = "the" w = "world" print l," ",t," ",w,".\n"printのところはこうも書ける。
print "#{l} #{t} #{w}.\n"実行
C:\>ruby test1.rb love the world. C:\>
"inFile.txt"というファイルを読込んで、内容を標準出力に出力する。だけ。
test2.rbopen("inFile.txt") { |io| io.each { |line| print line } }inFile.txt
abcdefg hijklmn opqrstu実行結果
C:\>ruby test2.rb abcdefg hijklmn opqrstu C:\>
"abc def ghi"という文字列を空白で分割してみる。
分割された文字列は配列になるので2つ目の文字列を表示してみる。
arry = "abc def ghi".split print arry[1]1行で実行
C:\ruby -e 'print "abc def ghi".split[1]' def","で分割する場合はこう。
C:\ruby -e 'print "abc, def, ghi".split(",")[1]' defprintのかわりにpを使うとこうなる。
C:\ruby -e 'p "abc, def, ghi".split(",")[1]' " def"先頭に空白が入っているので取る。
C:\ruby -e 'p "abc, def, ghi".split(",")[1].strip' "def"
print "ABC_DEF_GHI".split("_").map {|str| str.capitalize}.join実行
C:\ruby test3.rb AbcDefGhi
1文字目だけ小文字にしてみる
test3.rbs = "ABC_DEF_GHI".split("_").map {|str| str.capitalize}.join s[0,1] = s[0,1].downcase print s実行
C:\ruby test3.rb abcDefGhi
print "AbcDefGhi".scan(/[A-Z][a-z]*/).map {|str| str.upcase}.join("_")実行
C:\ruby test4.rb ABC_DEF_GHI