2016年3月29日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Ruby 2.3 (プログラミング言語)

7つの言語 7つの世界 (Bruce A. Tate (著)、まつもとゆきひろ (監訳)、田和 勝 (翻訳)、オーム社)の第1章(Ruby)、セルフスタディ1日目を取り組んでみる。

セルフスタディ1日目

コード(Emacs)

#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*-

puts 'Hello, world'

puts 'Hello, Ruby'.index 'Ruby'

puts 'Ruby' * 10

(1..10).each do |i|
  puts "This is sentence number #{i}"
end

2.times do
  num = rand 10
  print "Enter a nmber: (0-9) "
  guess = gets.chomp.to_i
  while num != guess
    if num < guess
      puts "high"
    else
      puts "low"
    end
    print "Enter a nmber: (0-9) "
    guess = gets.chomp.to_i
  end
  puts "end"
end

入出力結果(Terminal, irb)

$ ./sample1.rb 
Hello, world
7
RubyRubyRubyRubyRubyRubyRubyRubyRubyRuby
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
Enter a nmber: (0-9) 4
low
Enter a nmber: (0-9) 7
high
Enter a nmber: (0-9) 5
end
Enter a nmber: (0-9) 4
low
Enter a nmber: (0-9) 7
low
Enter a nmber: (0-9) 8
end
$

0 コメント:

コメントを投稿

Comments on Google+: