プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の8章(配列とイテレータ), 8.3(練習問題)、目次改訂版を解いてみる。
目次改訂版
コード(TextWrangler)
#!/usr/bin/env ruby #encoding: utf-8 book = [['start',1], ['number',11], ['string',15]] i = 1 width = 40 puts 'contents'.center(width) puts book.each do |chapter| puts ('chapter ' + i.to_s + ': ' + chapter[0]).ljust(width/2) + ('p.' + chapter[1].to_s).rjust(width/2) i += 1 end
入出力結果(Terminal)
$ ./ruby_program.rb contents chapter 1: start p.1 chapter 2: number p.11 chapter 3: string p.15 $
0 コメント:
コメントを投稿