2009年5月29日金曜日

ActionScriptでrubyのObject#inspectとp

RubyのObject#inspectp
inspect.as
package com.blogspot.takumakei.utils
{
import flash.utils.getQualifiedClassName;

public function inspect(x:*):String
{
if(x == null)
return "null";

if(x is Boolean)
return (x ? 'true' : 'false');

if(x is int || x is uint || x is Number)
return x.toString();

if(x is String)
return '"' + x + '"';

if(x is Array){
var r:String = '[';
if(0 < x.length){
r += inspect(x[0]);
for(var i:int = 1; i < x.length; ++i){
r += ',';
r += inspect(x[i]);
}
}
r += ']';
return r;
}

return '#<' + flash.utils.getQualifiedClassName(x) + ':"' + x.toString() + '">';
}
}

p.as
package com.blogspot.takumakei.utils
{
public function p(x:*):void
{
trace(inspect(x));
}
}

0 件のコメント:

コメントを投稿