2012/03/24

Code Prettifyを使った、ソースコード表示サンプル

Ruby: 行番号あり

<pre class="prettyprint linenums"></pre>
class ApplicationController < ActionController::Base
  class Vector2D
  attr_accessor :x, :y   # インスタンス変数@x, @yに対応するゲッタとセッタを定義
  def initialize(x, y)   # コンストラクタ
    @x = x; @y = y   # @がつくのがインスタンス変数(メンバ変数)
  end
  def ==(other_vec)   # いわゆる演算子オーバーライド
    other_vec.x == @x && other_vec.y == @y
  end
  def +(other_vec)
    Vector2D.new(other_vec.x + @x, other_vec.y + @y)
  end
  ...
end
vec0 = Vector2D.new(10, 20); vec1 = Vector2D.new(20, 30)
p vec0 + vec1 == Vector2D.new(30, 50) #=> true

PHP: 行番号なし

<pre class="prettyprint"></pre>
$personal_name = $_POST['personal_name'];
$contents = $_POST['contents'];
$contents = nl2br($contents);
print('投稿者:'.$personal_name.'
'); print('内容:
'); print(''.$contents.'
');
参考):http://www.kuribo.info/2008/04/code-prettify.html

0 件のコメント: