Download Ruby勉強会(第1回)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Ruby勉強会(第1回)
2006/06/29
竹内豪
Windows XPでRubyを使う
• 一番お気軽な方法
– バイナリをダウンロードしてきて解凍
http://www.garbagecollect.jp/ruby/mswin32/ja/
– フォルダを適当な名前にリネームしてCドライブ直下に移
動(例:rubyにリネームしてC:\rubyに移動)
– PATHを通す(PATH=ほげほげ;C:\ruby\bin;とか)
• irbを使う
– http://jarp.does.notwork.org/win32/からreadline.dllをダ
ウンロードしてきて、binフォルダと同じ場所に入れる
Rubyはオブジェクト指向言語である
#新しいオブジェクトの生成
song1 = Song.new(“Ruby Tuesday”)
song2 = Song.new(“Enveloped in Python”)
#定数もオブジェクトとして扱う
“gin joint”.length →9
“Rick”.index(“c”) →2
-1942.abs
→1942
sam.play(aSong)→”duh dum, dum de dum …”
Java → number = Math.abs(number)
Ruby→ number = number.abs
Rubyの基本(1)
#メソッドの作り方
def sayGoodnight(name)
result = “Goodnight, “ + name
return result
end
#文の終わりにセミコロンはつかない
puts sayGoodnight(“john-Boy”)
puts sayGoodnight(“Mary-Ellen”)
Rubyの基本(2)
#ここらへんはPerlとだいたい同じ
#右から順に評価されたり、カッコ省略したり
puts sayGoodnight “John-Boy”
puts sayGoodnight (“John-Boy”)
puts(sayGoodnight “John-Boy”)
puts(sayGoodnight(“John-Boy”))
puts “And Goodnight,\nGrandma”
Rubyの基本(3)
#ダブルクオート内での変数の評価
def sayGoodnight(name)
result = “Goodnight, #{name}”
return result
end
def sayGoodnight(name)
“Goodnight, #{name}”
end
Rubyの基本(4)
• 先頭文字について
– ローカル変数、メソッドの引数、メソッド名はすべ
て小文字またはアンダースコア(_)で始める
– グローバル変数にはドル記号($)をつける
– インスタンス変数にはアットマーク(@)をつける
– クラス変数には2つのアットマーク(@@)を先頭に
つける
– クラス名、モジュール名、定数は先頭文字を大文
字にする
配列とハッシュ(1)
#undefじゃなくnil
a = [1, ‘cat’, 3.14 ]
a[0] → 1
a[2] → nil
a
→ [1, “cat”, nil]
empty1 = []
empty2 = Array.new
配列とハッシュ(2)
a = %w{ ant bee cat dog elk }
a[0] → “ant”
a[3] → “dog”
instSection = {
‘cello’
=>
‘clarinet’ =>
‘drum’
=>
‘oboe’
=>
‘trumpet’ =>
‘violin’
=>
}
‘string’,
‘woodwind’,
‘percussion’,
‘woodwind’,
‘brass’,
‘string’,
配列とハッシュ(3)
instSection[‘oboe’]
→ “woodwind”
instSection[‘cello’]
→ “string”
instSection[‘bassoon’] → “nil”
#未定義値のデフォルト設定
histogram = Hash.new(0)
histogram[‘key1’] → 0
histogram[‘key1’] → historgram[‘key1’] + 1
histogram[‘key1’] → 1
制御構造(1)
#ブロックはendで終わらせる
If count > 10
puts “Try again”
elsif tries == 3
puts “You lose”
else
puts “Enter a number”
end
制御構造(2)
while weight < 100 and numPallets <= 30
pallet = nextPallets <= 30
weight += pallet.weight
numPallets += 1
end
if radation > 3000
puts “Danger, Will Robinson”
end
制御構造(3)
puts “Danger, will Robinson” if radiation > 3000
while square < 1000
square = square*square
end
square = square*square while square < 1000
正規表現(1)
#パッと見Perlと同じっぽい
/Perl|Python/
/P(erl|ython)/
/\d\d:\d\d:\d\d/
/Perl.*Python/
/Perl\s+Python/
/Ruby (Perl|Python)/
正規表現(2)
if line =~ /Perl|Python/
puts “Scripting language mentioned:
#{line}”
end
#Stringクラスの置換メソッド(substitute)
line.sub(/Perl/, ‘Ruby’)
line.sub(/Python/, ‘Ruby’)
ブロックとイテレータ(1)
#両方ともブロック
{puts “Hello” }
do
club.enroll(person)
person.socialize
end
ブロックとイテレータ(2)
#このyieldというのがよくわからない^^;
def callBlock
yield
yield
end
callBlock { puts “In the block” }
実行結果
In the block
In the block
ブロックとイテレータ(3)
def callBlock
yield “hello”, 99
end
callBlock { |str, num| …}
a = %w( ant bee cat dog elk )
a.each { |animal| puts animal}
ブロックとイテレータ(4)
def each
for each element
yield(element)
end
end
[ ‘cat’, ‘dog’, ‘horse’ ].each do |animal|
print animal, “ – “
end
ブロックとイテレータ(5)
#組み込みのイテレータ
5.times { print *** }
3.upto(6) {|i| print i }
(‘a’..’e’).each {|char| print char }
読み書き(1)
#おなじみprintf
printf “Number: %5.2f, String: %s”, 1.23,
“hello”
#1行キーボードから入力を読み込む
line = gets
print = line
読み書き(2)
while gets
if /Ruby/
print
end
end
ARGF.each { |line| print line if line =~
/Ruby/ }
参考文献
• 達人プログラマーズガイド プログラミング
Ruby
– デビット・トーマス+アンドリュー・ハント 著
– 田和 勝 訳
– まつもとゆきひろ 監修
– ピアソン・エデュケーション
• Rubyインストールガイド
– http://www.ruby-lang.org/ja/install.cgi
Related documents
List["1", "2", "3"]
List["1", "2", "3"]