|  | 
リテラルについて前にも調べたんだけど・・・。
"『Ruby ソースコード完全解説』第1章 Ruby言語ミニマム・様々なリテラル"では「直接オブジェクトを生成する式(リテラル)」と説明している。
「ソースコード内に値を直接表記したもの(Wikipedia)」
# 右がリテラル
a = 123
b = 3.14159265358979
c = "hello,world!"
d = [1,[2,3],4]
e = {:name=>'taro'}
Ruby はリテラルを読み込むとオブジェクトとして保持する。
p("hello".object_id)                #=> 7799950
p( 1.object_id)                     #=> 3
p({:name=>'taro'}.object_id)        #=> 7799920
数字の1や文字列"hello world"のようにRubyのプログラムの中に直接記述 できる値の事をリテラルといいます。
マニュアルが一番詳しいじゃん・・・orz
『Ruby ソースコード完全解説』第1章 Ruby言語ミニマム
p("content")
 
(snip)
 
ところで、pが書いてある場所はプログラムのネストレベルが一番低い、
つまりプログラム的には一番「上」なので、「トップレベル」と呼ばれて
いる。
なんとなくしか理解していなかったので。
『Ruby ソースコード完全解説』第1章 Ruby言語ミニマム
クラスとメソッド クラス 本来オブジェクト指向システムにおいてメソッドとはオブジェクトに所属 するものだ。だがそれはあくまで理想の世界でのこと。普通のプログラム なら同じメソッドの集合を持つオブジェクトがたくさんあるわけで、もし バカ正直にオブジェクト単位で呼び出せるメソッドを記憶していたりした ら大変なことになってしまう。そこで普通はクラスとかマルチメソッドの ような仕組みを利用して定義の重複をなくすわけだ。
「大変なことになってしまう」っていうのはインスタンスごとに特異メソッドを定義していくようなイメージを言っているのかな・・・。
「マルチメソッド」ってどんなのだろう・・・。
多重ディスパッチ(英: Multiple dispatch)またはマルチメソッド(英: Multimethods)とは・・・、
とりあえずリンクだけ。
$ sudo yum install fpc Setting up Install Process Setting up repositories dag 100% |=========================| 1.1 kB 00:00 kbs-CentOS-Extras 100% |=========================| 951 B 00:00 (snip) Added 32 new packages, deleted 0 old in 7.24 seconds Excluding Packages in global exclude list Finished Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only Finished Parsing package install arguments Resolving Dependencies --> Populating transaction set with selected packages. Please wait. ---> Downloading header for fpc to pack into transaction set. fpc-2.0.2-3.el4.kb.i386.r 100% |=========================| 142 kB 00:01 ---> Package fpc.i386 0:2.0.2-3.el4.kb set to be updated --> Running transaction check Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: fpc i386 2.0.2-3.el4.kb kbs-CentOS-Extras 15 M Transaction Summary ============================================================================= Install 1 Package(s) Update 0 Package(s) Remove 0 Package(s) Total download size: 15 M Is this ok [y/N]: y Downloading Packages: (1/1): fpc-2.0.2-3.el4.kb 100% |=========================| 15 MB 01:15 Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing: fpc ######################### [1/1] Installed: fpc.i386 0:2.0.2-3.el4.kb Complete!
Pascal で Hello, World!
$ cat hello.pas
program HelloWorld(output);
begin
  writeln('Hello, World!')
end.
 
$ fpc hello.pas
Free Pascal Compiler version 2.0.2 [2006/01/07] for i386
Copyright (c) 1993-2005 by Florian Klaempfl
Target OS: Linux for i386
Compiling hello.pas
Linking hello
5 Lines compiled, 0.6 sec
  
$ ls hell*
hello*  hello.o  hello.pas
$ ./hello
Hello, World!
引数の output は何?
●SourceForge: apollo/apollo/Rakefile
require 'win32/registry'
ruby_ap_path = Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\ruby_ap.exe') { |reg| reg[''] }
FileUtils::RUBY.replace(ruby_ap_path)
レジストリはこうやって読むのか・・・。
$ cat add.c
int add(a, b)
     int a, b;
{
  return( a + b );
}
 
$ cat test2.c
#include "ruby.h"
 
int add(int a, int b);
 
VALUE wrap_add(self, aa, bb)
     VALUE self, aa, bb;
{
  int a, b, result;
 
  a = FIX2INT(aa);
  b = FIX2INT(bb);
  result = add(a,b);
  return INT2FIX(result);
}
 
void Init_test()
{
  VALUE module;
 
  module = rb_define_module("Test");
  rb_define_module_function(module, "add", wrap_add, 2);
}
 
$ cat extconf.rb
require "mkmf"
create_makefile("test")
 
$ ruby extconf.rb
creating Makefile
 
$ make
gcc -I. -I/usr/lib/ruby/1.8/i386-linux -I/usr/lib/ruby/1.8/i386-linux -I.  -fPIC -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall  -fPIC  -c test2.c
gcc -I. -I/usr/lib/ruby/1.8/i386-linux -I/usr/lib/ruby/1.8/i386-linux -I.  -fPIC -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall  -fPIC  -c add.c
gcc -shared -o test.so test2.o add.o -L"." -L"/usr/lib" -L.  -rdynamic -Wl,-export-dynamic    -lruby  -lpthread -ldl -lcrypt -lm   -lc
 
$ ruby -r test -e 'p Test.add(1,2)'
3
$ irb --simple-prompt
>> require 'test'
=> true
>> Test.methods(false)
=> ["add"]
>> Test.ancestors
=> [Test]
>> Test.class
=> Module
>> Test.add(2,3)
=> 5
$ rpm -qf /usr/share/doc/ruby-devel-1.8.6.36/README.EXT.ja ruby-devel-1.8.6.36-3.fc7 $ rpm -ql ruby-devel /usr/lib/libruby-static.a /usr/lib/libruby.so /usr/lib/ruby/1.8/i386-linux/config.h /usr/lib/ruby/1.8/i386-linux/defines.h /usr/lib/ruby/1.8/i386-linux/digest.h /usr/lib/ruby/1.8/i386-linux/dl.h /usr/lib/ruby/1.8/i386-linux/dlconfig.h /usr/lib/ruby/1.8/i386-linux/dln.h /usr/lib/ruby/1.8/i386-linux/env.h /usr/lib/ruby/1.8/i386-linux/intern.h /usr/lib/ruby/1.8/i386-linux/missing.h /usr/lib/ruby/1.8/i386-linux/node.h /usr/lib/ruby/1.8/i386-linux/re.h /usr/lib/ruby/1.8/i386-linux/regex.h /usr/lib/ruby/1.8/i386-linux/ruby.h /usr/lib/ruby/1.8/i386-linux/rubyio.h /usr/lib/ruby/1.8/i386-linux/rubysig.h /usr/lib/ruby/1.8/i386-linux/st.h /usr/lib/ruby/1.8/i386-linux/util.h /usr/lib/ruby/1.8/i386-linux/version.h /usr/share/doc/ruby-devel-1.8.6.36 /usr/share/doc/ruby-devel-1.8.6.36/README.EXT /usr/share/doc/ruby-devel-1.8.6.36/README.EXT.ja
sumim’s smalltalking-tos - Ruby の落とし穴 3
後で読む。
2007/10/06 : 
Martin Fowler's Bliki in Japanese - クラスインスタンス変数
こっちが面白かった。Ruby 1.9 では継承されないようになったということでめでたし、めでたし。
class Employee
  @@instances = {}
  def self.instances
    @@instances[self]
  end
  def store
    @@instances[self.class] ||= []
    @@instances[self.class] << self
  end
  def initialize name
    @name = name
  end
end
 
class Overhead < Employee; end
class Programmer < Employee; end
 
Overhead.new('Martin').store
Overhead.new('Roy').store
Programmer.new('Erik').store
puts Overhead.instances.size
puts Programmer.instances.size
インスタンスが生成されていない段階でもクラス変数に蓄積されていく。
2007/10/07 : new で生成はされているが代入、保存されていない。
$ irb --simple-prompt
>> RUBY_VERSION
=> "1.9.0"
>> require 'pp'
=> true
>> h=h = {:a => 'a'*5, :b => 'b'*10, :c => 'c'*20, :d => 'd'*30}
=> {:a=>"aaaaa", :b=>"bbbbbbbbbb", :c=>"cccccccccccccccccccc", :d=>"dddddddddddddddddddddddddddddd"}
>> pp h
{:a=>"aaaaa",
 :b=>"bbbbbbbbbb",
 :c=>"cccccccccccccccccccc",
 :d=>"dddddddddddddddddddddddddddddd"}
=> nil
>> puts h.inspect
{:a=>"aaaaa", :b=>"bbbbbbbbbb", :c=>"cccccccccccccccccccc", :d=>"dddddddddddddddddddddddddddddd"}
>> puts h.pretty_inspect
{:a=>"aaaaa",
 :b=>"bbbbbbbbbb",
 :c=>"cccccccccccccccccccc",
 :d=>"dddddddddddddddddddddddddddddd"}
=> nil
>> puts h.pretty_print_inspect
{:a=>"aaaaa", :b=>"bbbbbbbbbb", :c=>"cccccccccccccccccccc", :d=>"dddddddddddddddddddddddddddddd"}
=> nil
=> nil
>> puts h.pretty_print_inspect
{:a=>"aaaaa", :b=>"bbbbbbbbbb", :c=>"cccccccccccccccccccc", :d=>"dddddddddddddddddddddddddddddd"}
=> nil
●NPHスクリプト(とほほのCGI入門 > CGIその他)
夏原稿2001 〜 mod_ruby入門? にて NPH(non-parsed header)スクリプトなるものを知った。
CGI#gateway_interface 
 CGI:"CGI/1.1"、mod_ruby:"CGI-Ruby/1.1" になる。
$ cat mod_ruby_test.rhtml
<% ERuby.charset = "EUC-JP"
require 'cgi'
 
c = CGI.new('html4Tr')
 
c.out do
  c.html {
    c.head { c.title{'sample1'} } + c.body {
      c.p {'GATEWAY_INTERFACE: ' + (c.gateway_interface || '(null)')} +
        c.br + c.ul {
        c.params.collect{|k, v|
          c.li {CGI::escapeHTML(k) + ': ' + CGI::escapeHTML(v.inspect)}
        }
      }
    }
  }
end
%>
●ハイパーテキストトランスファープロトコール第1.0版仕様書(日本語翻訳版)
●RFC・勸告・仕樣書その他(Love Cream Puff)
require 'timeout'
 
begin
  timeout(3) do
    1000.times do |num|
      p num
      sleep(0.5)
    end
  end
rescue TimeoutError
  p "TimeoutError :#{$!}"
end
 
__END__
#=>
0
1
2
3
4
5
"TimeoutError :execution expired"
$ cat calc.l
%%
"+"             return(ADD);
"-"             return(SUB);
"*"             return(MUL);
"/"             return(DIV);
"("             return(LP);
")"             return(RP);
[0-9]+          { yylval = atoi(yytext);
                  return(NUMBER); }
[ \t]           ;
\n              return(NL);
.               return( yytext[0]);
%%
$ lex calc.l $ ls calc.l lex.yy.c
$ cat calc.y
%token  NL
%token  NUMBER
%token  LP
%token  RP
%left   ADD SUB
%left   MUL DIV
%%
s               : list
                ;
list            : /* empty */
                | list expr NL  { printf("%d\n",$2); }
                ;
expr            : expr ADD expr { $$ = $1 + $3; }
                | expr SUB expr { $$ = $1 - $3; }
                | expr MUL expr { $$ = $1 * $3; }
                | expr DIV expr { $$ = $1 / $3; }
                | LP expr RP    { $$ = $2; }
                | NUMBER        { $$ = $1; }
                ;
%%
# include "lex.yy.c"
$ yacc calc.y $ ls calc.y calc.l lex.yy.c y.tab.c $ gcc y.tab.c -ly -ll -o calc $ ls calc calc.y calc.l lex.yy.c y.tab.c
$ ./calc 1+1 2 1+2+3-5 1 8+4*5 28 5*(3+4)/2 17
$ cat start_stop.l
%{
#include 
%}
 
%%
stop    printf("Stop command received\n");
start   printf("Start command received\n");
%%
 
$ lex start_stop.l
$ gcc lex.yy.c  -ll -o start_stop
$ ./start_stop
stop
Stop command received
 
start
Start command received
 
hoge
hoge
 $ cat hello.c #includevoid hello() { printf("Hello, world!\n"); } $ cat test1.c #include "ruby.h" void hello(); VALUE wrap_hello(self) VALUE self; { hello(); return Qnil; } void Init_test() { VALUE module; module = rb_define_module("Test"); rb_define_module_function(module, "hello", wrap_hello, 0); } $ cat extconf.rb require 'mkmf' create_makefile('test') $ ruby extconf.rb creating Makefile $ ls Makefile extconf.rb hello.c test1.c $ make gcc -I. -I/usr/lib/ruby/1.8/i386-linux -I/usr/lib/ruby/1.8/i386-linux -I. -fPIC -O2 -m32 -march=i386 -mcpu=i686 -fPIC -c hello.c gcc -I. -I/usr/lib/ruby/1.8/i386-linux -I/usr/lib/ruby/1.8/i386-linux -I. -fPIC -O2 -m32 -march=i386 -mcpu=i686 -fPIC -c test1.c gcc -shared -rdynamic -Wl,-export-dynamic -L'/usr/lib' -Wl,-R'/usr/lib' -o test.so hello.o test1.o -Wl,-R -Wl,/usr/lib -L/usr/lib -L. -lruby -lpthread -ldl -lcrypt -lm -lc $ ruby -r test -e 'Test.hello' Hello, world! 
$ irb irb(main):001:0> require 'test' => true irb(main):003:0> Test.methods(false) ["hello"] irb(main):004:0> Test.class Module irb(main):006:0> Test.ancestors [Test] irb(main):007:0> Test.hello Hello, world! => nil
| 前 | 2007年 10月 | 次 | ||||
| 日 | 月 | 火 | 水 | 木 | 金 | 土 | 
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 | 
| 14 | 15 | 16 | 17 | 18 | 19 | 20 | 
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | 
| 28 | 29 | 30 | 31 | |||