[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[dennou-ruby:000246] Re: NumArray prototype in ruby
In message "[dennou-ruby:000241] Re: NumArray prototype in ruby"
on 00/02/07, Masato Shiotani <shiotani@xxxxxx> writes:
>����ɤ���äƻȤ���?
>
>���Ȥ���Υ����ڤ���, #! /usr/local/bin/ruby �Ĥ��Ƽ¹Բ�ǽ
>�ˤ�,
>
> % xmp.rb numarray.rb
>
>�Ȥ���äƤ�, ������Ϥ���ޤ���...
���������ä� raa ����귫�äƤ�餦��ʬ�����Ǥ�����
xmp <<-EOS
a = "The moon Fly"
b = [a.split, "TO", "Me"]
b = b.flatten
b.sort!
b.join(" ").capitalize
EOS
�Ȥ���ȡ�
a = "The moon Fly"
#=> "The moon Fly"
b = [a.split, "TO", "Me"]
#=> [["The", "moon", "Fly"], "TO", "Me"]
b = b.flatten
#=> ["The", "moon", "Fly", "TO", "Me"]
b.sort!
#=> ["Fly", "Me", "TO", "The", "moon"]
b.join(" ").capitalize
#=> "Fly me to the moon"
�Ƚ��Ϥ���ޤ���if __FILE__ == $0 �ˤ⤰����ޤ��ƻȤ��Τ�
������ӤǤ���
-- gotoken
# �㳰����ʣ���Խ��ϡ�����ӥե����ޥåȥ������ޥ����Ƥߤޤ�����
def xmp(arg, fmt = "%s\n #%s\n", sep="\n # ")
if fmt
fmt = fmt.to_s
eval("__sep__ = #{sep.inspect}; __res__ = []", TOPLEVEL_BINDING)
eval(arg.gsub(/^(.*)\n?/){
%Q|
begin
__res__ << "=> " + (#{$1}).inspect.gsub(/\n^/, __sep__);
rescue
__res__ << "!! " + $!;
end|}, TOPLEVEL_BINDING)
arg.split(/\n/).each_with_index{|l,i|
(puts "\n" ; next) if l =~ /^$/
print format(fmt, l, eval("__res__", TOPLEVEL_BINDING)[i])
}
else
print arg; eval arg
end
end