たのしいRuby

single quote と double quote は意味が違う

print("print1 hello\truby\n")
puts
print('print2 hello\truby\n')
puts
print1 hello    ruby

print2 hello\truby\n


文字列接続

print('print3 hello' , 'ruby')
puts
print('print4 hello' + 'ruby')
puts
print3 helloruby
print4 helloruby


puts(改行付き)は上と結果が違う

puts('puts1 hello' , 'ruby')
puts
puts('puts2 hello' + 'ruby')
puts
puts1 hello
ruby

puts2 helloruby


p 命令は文字列なら double quoteで囲む

p('p1 hello' , 'ruby')
puts
p("p2 hello" + "ruby")
puts
"p1 hello"
"ruby"

"p2 helloruby"


数学関数

print("sin(3.1415)=", Math.sin(3.1415), "\n");
print("sqrt(10000)=", Math.sqrt(10000), "\n");
sin(3.1415)=9.26535896604902e-005
sqrt(10000)=100.0


if文 thenは必要ない

a = 1
if (a==2) then
   p("a==2")
else
   p("a!=2")
end
"a!=2"


while文 doは必要ない

while a<10 do
  a+=1
  print("a=",a,"\n")
end
a=2
a=3
a=4
a=5
a=6
a=7
a=8
a=9
a=10


timesメソッド

c=0
a.times{
  print("  up!",c)
  c+=1
}
  up!0  up!1  up!2  up!3  up!4  up!5  up!6  up!7  up!8  up!9


def

def newprint
    print("newprint")
end
newprint()
newprint


別ファイル読み込み 「.rb」を省略可能

require "ruby20090407-1753.rb"
newprint()

別ファイル:
def newprint
    print "rubyruby\n"
end
rubyruby

2章

配列

array1=['abc', 'def', 1]
puts array1[0]

array1[10] = 'xyz'
abc


配列大きさ

p 'arraysize='+array1.length.to_s
p 'arraysize='+array1.size.to_s
"arraysize=11"
"arraysize=11"


配列すべて ←イテレータ {} はブロック eachはブロック付きメソッド

array1.each{|a|
  print "#"; p a
}
#"abc"
#"def"
#1
#nil
#nil
#nil
#nil
#nil
#nil
#nil
#"xyz"


ハッシュ

hash2={'S'=>-1, 'M'=>0, 'L'=>1, 'LL'=>2, 'X'=>3}
print('S => ',  hash2['S'],"\n")
hash2.each{|a,b|
  print(a,' size means.. ', b,"\n")
}
S => -1
LL size means.. 2
L size means.. 1
X size means.. 3
M size means.. 0
S size means.. -1
p hash2
puts
require 'pp'
pp hash2
{"LL"=>2, "L"=>1, "X"=>3, "M"=>0, "S"=>-1}

{"LL"=>2, "L"=>1, "X"=>3, "M"=>0, "S"=>-1}


正規表現

if /R/i =~ "Ruby"
  puts "pattern 'R' matched the strings 'Ruby'."
else
  puts "pattern 'R' do not matched the strings 'Ruby'."
end
pattern 'R' matched the strings 'Ruby'.


引数の扱い

c=0
ARGV.each {|arg|
  print(c,":'", arg, "'\n")
  c+=1          
}
print("ARGV[0]=",ARGV[0],"\n")
$ ruby argtest.rb abc def xyz
0:'abc'
1:'def'
2:'xyz'
ARGV[0]=abc


ファイル読み込み(一気に)

fname=ARGV[0] # file name
fh=open(fname) #file handle
ftext=fh.read #file text
print ftext
fh.close
$ ruby filereadall.rb b.txt
ロミオとジュリエット(Romeo and Juliet、1595 - 96年)
ジュリアス・シーザー(Julius Caesar、1599年)
ハムレット(Hamlet、1600 - 01年)
夏の夜の夢(A Midsummer Night's Dream、1595年 - 96年)
ヴェニスの商人(The Merchant of Venice、1596年 - 1597年)
空騒ぎ(Much Ado About Nothing、1598年 - 1599年)
お気に召すまま(As You Like It、1599年)


ファイル読み込み(一行づつ)

fname=ARGV[0] # file name
fh=open(fname) #file handle
while line=fh.gets
  print "> ", line
end
fh.close
$ ruby filereadline.rb b.txt
> ロミオとジュリエット(Romeo and Juliet、1595 - 96年)
> ジュリアス・シーザー(Julius Caesar、1599年)
> ハムレット(Hamlet、1600 - 01年)
> 夏の夜の夢(A Midsummer Night's Dream、1595年 - 96年)
> ヴェニスの商人(The Merchant of Venice、1596年 - 1597年)
> 空騒ぎ(Much Ado About Nothing、1598年 - 1599年)
> お気に召すまま(As You Like It、1599年)


grepもどき

pat = Regexp.new(ARGV[1]) #pattern
fname=ARGV[0] # file name
fh=open(fname) #file handle
while line=fh.gets
  if pat =~ line
    print "hit> ", line
  end
end
fh.close
$ ruby grep.rb b.txt 99
hit> ジュリアス・シーザー(Julius Caesar、1599年)
hit> 空騒ぎ(Much Ado About Nothing、1598年 - 1599年)
hit> お気に召すまま(As You Like It、1599年)

4章

#変数
# local
#   a, _a
# global
#   $g
# instance
#   @i
# class
#   @@c
# pseudo
#   true,false,self,..

#定数
#  ARGV,RUBY_VERSION,..

#予約語
#  if,end,cond, next


オブジェクトID=ポインタ?

  ar1=['a']
  ar2=['a']
  ar3=ar1
  p ar1.object_id
  p ar2.object_id
  p ar3.object_id

  print "ar1 and ar3 ID is same? "
  p ar1.equal?(ar3)

  print "ar1 and ar2 ID is same? "
  p ar1.equal?(ar2)

  print "ar1 and ar2 value is same? "
  p ar1 == ar2

  print "ar1 and ar2 value(no conversion of number) is same? "
  p ar1.eql?(ar2)
23212436
23212412
23212436
ar1 and ar3 ID is same? true
ar1 and ar2 ID is same? false
ar1 and ar2 value is same? true
ar1 and ar2 value(no conversion of number) is same? true


条件分岐

x=-321
if x>=1 && x<=10
  p "1 x="+x.to_s
elsif x>=11 && x<=20
  p "2 x="+x.to_s
else
  p "3 x="+x.to_s
end
"3 x=-321"


関数

def foo
  fh = open("c:/home/b.txt")
  print fh.read
end

def bar
  foo()
end

begin
  bar()
rescue => ex
  print "Disney says '", ex.message,"'\n"
end
$ ruby fileopen.rb
ロミオとジュリエット(Romeo and Juliet、1595 - 96年)
ジュリアス・シーザー(Julius Caesar、1599年)
ハムレット(Hamlet、1600 - 01年)
夏の夜の夢(A Midsummer Night's Dream、1595年 - 96年)
ヴェニスの商人(The Merchant of Venice、1596年 - 1597年)
空騒ぎ(Much Ado About Nothing、1598年 - 1599年)
お気に召すまま(As You Like It、1599年)


$ ruby fileopen.rb (b.txtを削除してから実行)
Disney says 'No such file or directory - c:/home/b.txt'