10 January, 2011

[7-Lang][Ruby] Seven Languages in Seven Weeks : Ruby Day 1

 Bruce TateのSeven Languages in Seven Weeksを読み始めました。
 最初の言語はRubyです。各言語をキャラクターに例えているのですが、Ruby = Merry Poppins らしい。
 1日目の課題はこんな感じでOKなのかしらん。

>> puts 'Hello, World!'
Hello, World!
=> nil
>> p "Hello, Ruby".index("Ruby")
7
=> nil
>> puts "@tmiya_\n"*10
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
@tmiya_
=> nil
>> (1..10).each {|n| puts "This is sentence number #{n}" }
This is sentence number 1
This is sentence number 2
This is sentence number 3
This is sentence number 4
This is sentence number 5
This is sentence number 6
This is sentence number 7
This is sentence number 8
This is sentence number 9
This is sentence number 10
=> 1..10

% ruby numberGuess.rb
5
too large
3
too large
1
too small
2
correct!
% cat numberGuess.rb
randomNumber = rand(10)
while ((guess = gets.to_i) != randomNumber)
if guess < randomNumber then
puts "too small"
else
puts "too large"
end
end
puts "correct!"

No comments: