sevk sevk
关注数: 30 粉丝数: 57 发帖数: 1,004 关注贴吧数: 22
用C写自己的ruby类库 用C写自己的ruby类库 下载ruby源码: linux: apt-get install ruby1.9.1-dev .最简单. windows: http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.ruby-lang.org%2Fzh_cn%2Fdownloads%2F&urlrefer=cd2266c3dc467c958d47cf9b40de7dec , 添加 include 和 lib 的环境变量. 编辑MyTest/MyTest.c : C代码 // Include the Ruby headers and goodies   #include "ruby.h"      // Defining a space for information and references about the module to be stored internally   VALUE MyTest = Qnil;      // Prototype for the initialization method - Ruby calls this, not you   void Init_mytest();      // Prototype for our method 'test1' - methods are prefixed by 'method_' here   VALUE method_test1(VALUE self);      // The initialization method for this module   void Init_mytest() {      MyTest = rb_define_module("MyTest");      rb_define_method(MyTest, "test1", method_test1, 0);      }      // Our 'test1' method.. it simply returns a value of '10' for now.   VALUE method_test1(VALUE self) {      int x = 10;      return INT2NUM(x);   }    编辑 MyTest/extconf.rb : Ruby代码 # Loads mkmf which is used to make makefiles for Ruby extensions   require 'mkmf'      # Give it a name   extension_name = 'mytest'      # The destination   dir_config(extension_name)      # Do the work   create_makefile(extension_name)   运行 kk@ubuntu:~/MyTest$ ruby extconf.rb creating Makefile 运行 kk@ubuntu:~/MyTest$ make gcc -I. -I/usr/include/ruby-1.9.1/i486-linux -I/usr/include/ruby-1.9.1/ruby/backward -I/usr/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64 -fPIC -fno-strict-aliasing -g -g -O2 -O2 -g -Wall -Wno-parentheses -fPIC -o test.o -c test.c gcc -shared -o mytest.so test.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic -lruby-1.9.1 -lpthread -lrt -ldl -lcrypt -lm -lc 当前目录生成了 mytest.so 文件 这个 mytest.so 就可以用 ruby load 或 require 了. Ruby代码 require 'mytest' ; include MyTest ; puts test1    输出 => 10    Ruby代码 # Load in the extension (on OS X this loads ./MyTest/mytest.bundle - unsure about Linux, possibly mytest.so)   require 'mytest'      # MyTest is now a module, so we need to include it   include MyTest      # Call and print the result from the test1 method   puts test1      # => 10  
ruby 中文编程 Ruby 1.9: 中文编程 突然想起,Ruby 1.9支持中文方法名和变量名! Ruby代码 def 召唤 家丁      case 家丁      when '阿福', '旺财'        puts "……少爷,我系#{家丁}……"      else        puts '……(一段短短的沉默,然后一段长长的沉默)'      end   end      家丁甲, 家丁乙 = %w[阿福 旺财]   召唤 家丁甲   哼哼,我们可以改造黄瓜或者阿死别克了 Ruby代码 alias 龙门阵 Story   alias 角色扮演 Scenario   alias 假设 Given   alias 当 When   alias 而且 And   alias 于是 Then   不过还是有些限制,类和模块不能用中文名打头(开头加上大写字母还是可以的) Ruby代码 class 家丁   end   #=>error: class/module name must be CONSTANT      class Q宝宝   end   #ok   观众可能会问: if then 怎么整?先想想,大致用法应该是这样吧 Ruby代码 如果 女的, 那么{问三围}, 否则{讲再见}    实现起来,我们可以定义如果-那么-否则如下 Ruby代码 def 那么 &块      块   end      def 否则 &块      块   end      def 如果 条件, 真块, 假块=->(){nil}      条件 ? 真块.call : 假块.call   end   试一试 Ruby代码 def 问三围      puts '小……小柠檬?!小蜜瓜?!不想活了?!'   end      def 讲再见      puts '不是讲好一小时见血任做吗?你跑不掉的……hehehe……'   end      [true, false].each {|女的|      如果 女的, 那么{问三围}, 否则{讲再见}   }   继续汉化def——很简单 Ruby代码 alias 定义 define_method   文章太长不好,就此打住~ 参考文献: 《窈窕淑女》,《零之使魔》,《买凶拍人》
修改源并更新你的Kubuntu/Ubuntu/Xubuntu [编辑] 修改源并更新你的Kubuntu/Ubuntu/Xubuntu不同的网络状况连接以下源的速度不同,所以cn99并不一定是最快的源。建议在添加前手动验证以下源的连接速度(ping下就行)。比如说北京网通用户连接cn99就非常慢,而ftp.sjtu.edu.cn是相对较快的源。选择最快的源可大大节省下载时间,请根据自己网络环境设置更新服务器,以达到最快的速度。 在kubuntu下,你可以使用Adept软件管理器,Ubuntu/Xubuntu下可以使用新立得软件管理器,命令行下可以使用apt-get install,aptitude install来安装软件。 注意,你可以同时加入几个源。尽量选择一组官方的源(也就是下面的Archive.ubuntu.com的条目)直接加在文件的最後,以避免非官方源软件包不全时出现 404 Not Found 文件未发现的错误。建议电信用户使用cn99和台湾大学的源,建议网通用户使用台湾大学的源。 关于终端:可以简单认为终端是在图形界面下的命令行操作模拟器,下文中出现的所有命令均需在终端中执行,你可以直接复制文中的命令行到终端中,回车运行它。但我建议你还是自己动手输入,作为开始进入linux世界的热身。 'Kubuntu'下打开终端方法: 按下ALT+F2 -> konsole -> 运行'Ubuntu'下打开终端方法: 按下ALT+F2 -> gnome-terminal -> 运行备份当前的源列表,以便日後需要时恢复: sudo cp /etc/apt/sources.list /etc/apt/sources.list_backupKubuntu下可以使用Kate作为文本编辑软件,Ubuntu下可以用gedit作为文本编辑软件,当然你可以换成任一款你喜欢的编辑器,如leafpad,mousepad,vim,scribes,nano等。 在Kubuntu下尽量使用 kdesu 命令 来获取管理员权限并执行某指令,Ubuntu下尽量使用 gksu 命令 来获取管理员权限并执行某指令。 编辑源列表,将对于你最快的源放在最前面,如果有alternateCD/DVD则将其放于最开始位置,如果你还没有学会如何挂载ISO光盘镜像,那就先在该行前面加上一个#号,或者先去学习一下关于mount命令的用法。 kubuntu: kdesu kate /etc/apt/sources.listubuntu: gksu gedit /etc/apt/sources.listxubuntu: gksu mousepad /etc/apt/sources.list从以下各服务器列表内容中选择一段替换文件中的所有内容,为防止非官方源中软件包不全的问题,请在sources.list文件中尾部添加一组官方源。
首页 1 2 下一页