2012年10月4日木曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 8章(配列とイテレータ), 8.3(練習問題)、目次改訂版 を解いてみる。

その他参考書籍

目次改訂版

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

contents = [
  ["start",1],
  ["number",11],
  ["string",15]]

width = 40
puts "table of contents".center(40)
puts
i = 1
contents.each do |content|
  puts "chapter #{i} #{content[0]}".ljust(width/2) + "p.#{content[1]}".rjust(width/2)
  i += 1
end

入出力結果(Terminal)

$ ./sample.rb
           table of contents            

chapter 1 start                      p.1
chapter 2 number                    p.11
chapter 3 string                    p.15
$

日本語で上手く揃える方法がわからないのでとりあえず英語で。

0 コメント:

コメントを投稿