åå§çRubyå ¥é
æè¿ãRubyãæ¸ãã¦ããªãã®ã§åå ¥éãããã¨æããHello World ã®ãã¥ã¼ããªã¢ã«ãæ¸ããã¨æãã
ã¾ããRubyãå ¥æãããここããææ°ã®ã½ã¼ã¹ãå ¥æããã³ã³ãã¤ã«ãã¦ããããä½äºãæ°ããã®ã¯ãããã¨ã ã¨æããã¨ãããããä»åã¯ãå®å®çã®ææ°ã½ã¼ã¹ãå ¥æã
次ã«ãã好ã㪠vim ã§æ°ãããã¡ã¤ã«ãä½ãããã¡ã¤ã«ãã¤ããã®ã¯ãå ã»ã©è½ã¨ããRubyã®ã½ã¼ã¹ã¨åããã£ã¬ã¯ããªãããã ããããã¡ã¤ã«ã®å é ã«ã¯ãããã¾ãã®ãã¾ããªããæ¸ããªãã¦ã¯ãããªãã
#include "ruby.h"
ããã¯ãRubyãåå¼·ãã¦ãããã¡ã«ç解ã§ããã¨æãã
Rubyã¯ãå®å ¨ãªãªãã¸ã§ã¯ãæåè¨èªãªã®ã§(ä¿¡ããããããï¼æ°å¤ãæååã¾ã§ãªãã¸ã§ã¯ããªãã ãJavaã®ããã«ããªããã£ããªåãªãã¦åå¨ããªããï¼ãã¾ãã¯ãªãã¸ã§ã¯ããä½æããªããã°ãªããªããä½æã®æ¹æ³ã¯ã
VALUE my_obj = rb_class_new_instance(0,0,rb_cObject);
ããã§ãObjectã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹my_objãä½æãããã
次ã«ãä»åã®Hello Worldããã°ã©ã ã§ã¯ãæ¨æºåºåã«æåãæ¸ãåºããªãã¨ãããªããRubyã§ã¯ãKernelã¢ã¸ã¥ã¼ã«ã®ã¡ã½ããã¨ãã¦ãputsã¨ããã®ãç¨æããã¦ãããå®ã¯ãObjectã¯ã©ã¹ã«ã¯ãæåããKernelã¢ã¸ã¥ã¼ã«ãã¤ã³ã¯ã«ã¼ãããã¦ããã®ã§ãä½ãããªãã¦ãã(ä½ããã£ã¦ãããããããªãã¦ã大ä¸å¤«ããã®ãã¡ãããããã)
次ã«ãputsã¡ã½ãããå¼ã³åºãããã®ãã¾ããªãã
int method_id = rb_intern("puts");
ããããã¾ããªããªã®ã§ãä½ããã£ã¦ãããããããªãã¦ã大ä¸å¤«ãï¼Rubyã¯ã¡ã½ãããIDã§ç®¡çãã¦ãããã ãããã¯ã"puts"ã®IDãã¨ãã ãã¦ãã)
ããã¦ãå®éã«ã¡ã½ãããå¼ã³åºãã
rb_funcall(my_obj,method_id,1,rb_str_new2("hello world"));
å¼æ°ã¯é ã«ããªãã¸ã§ã¯ãããã¾ããªãã§åãåºããIDãã¡ã½ããã®å¼æ°ã®æ°ãã¡ã½ããã¸ã®å¼æ°...ã«ãªããä»åãããã¦ããå¼æ°ã§ä½¿ã£ã¦ããrb_str_new2ã¨ããã®ã¯ã(nullçµç«¯ããã¦ãã)æååãæååã¯ã©ã¹ã«å¤ãã¦ãããä¸ã§ãæ¸ãããã©ãæååãã¯ã©ã¹ãªãã ã
ããã§ãéè¦ãªé¨åã¯çµããããã¨ã¯ãããã¤ããã¾ããªããæ¸ãæãããã°å®æãæçµçã«ãã¡ã¤ã«ã¯ããã®ããã«ãªãã
#include "ruby.h" int main() { VALUE obj; int method_id; ruby_init(); ruby_init_loadpath(); ruby_script("hello_ruby"); my_obj = rb_class_new_instance(0,0,rb_cObject); method_id = rb_intern("puts"); rb_funcall(my_obj,method_id,1,rb_str_new2("hello world")); ruby_finalize(); return 0; }
ãã¦ãããããã楽ãã¿ã®å®è¡ã ãshellã«æ¬¡ã®ããã«æã¡è¾¼ããã ã
$gcc -o hello_ruby -I${Ruby_DIR} -g -L${Ruby_DIR} -lruby -ldl hello_ruby.c && ./hello_ruby hello world
${Ruby_DIR}ã¨æ¸ãã¦ããé¨åã¯ãé©å®ãã¨ããRubyã®ãã£ã¬ã¯ããªãæãããã«ãã¦å®è¡ãã¦ã¿ããã
ä»åæ¸ãã¦ã¿ã¦ããã£ãã¨æããã©ãRubyãæ¸ãã«ã¯ãã¾ããªãããã£ã±ãå¿ è¦ã«ãªãããã£ã¨ããããRubyã¯é»éè¡ãã©ãããããããããããªãã ãããã
çµããã«ï¼
ã¤ãã«ãã¨ãªã£ã¦ãã£ããããæ¸ãåã«ãæèªãã Rubyã®æ¬ã«ãHello Worldãã¤ã³ãã£ã¢ã³ã®æã¨ããHello Worldæ¸ãã¦åããã¦ãæå®ãããã¨ãæ¸ããã¦ããè¨æ¶ããã£ã¦ããããåç
§ãããã¨æã£ããã ãã©ãã©ããã¦ããã®æ¬ãæãåºããªãã£ããèªåãæåã«Rubyãæ¸ããã£ããã«ãªã£ãã256æ¬ç¶²éç·¨ã§æ¸ããã¦ãããã¨ãã£ã¨æã£ã¦ãããã ãã©éã£ããã ããç¥ã£ã¦ãããæãã¦ãã ããã
ãã¨ã調ã¹ãã®ã«Rubyã¢ããªã±ã¼ã·ã§ã³ããã°ã©ãã³ã°ã£ã¦æ¬ãæ¢ããããå¹³æ14å¹´ã®æ¬ãªã®ã«ãã¡ããã£ã¨ã ããã¹ããã¡ã¼ã¹ãããã°ã©ãã³ã°ã¨ãã®ã£ã¦ãã¦é©ãããã©ããããã¹ããã¡ã¼ã¹ãã£ã¦ããã¨æè¿ã®ã¤ã¡ã¼ã¸ããããããªãããããã ãããªã¼ã 社ã
ããã°ã©ãã³ã°Ruby 第2ç è¨èªç·¨
- ä½è : Dave Thomas,Chad Fowler,Andy Hunt,ã¾ã¤ãã¨ããã²ã,ç°åå
- åºç社/ã¡ã¼ã«ã¼: ãªã¼ã 社
- çºå£²æ¥: 2006/08/26
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 7人 ã¯ãªãã¯: 270å
- ãã®ååãå«ãããã° (152件) ãè¦ã
Rubyã¢ããªã±ã¼ã·ã§ã³ããã°ã©ãã³ã°
- ä½è : åç°ä¿®å¾,ã¾ã¤ãã¨ããã²ã,ãã¾ã ããã,æ°¸äºç§å©
- åºç社/ã¡ã¼ã«ã¼: ãªã¼ã 社
- çºå£²æ¥: 2002/04/01
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 3人 ã¯ãªãã¯: 45å
- ãã®ååãå«ãããã° (27件) ãè¦ã