Ruby 语言 思想驱动生活

Ruby,Rails,编程是一种乐趣。

Tag List for:

LuRuJu 将jude的模型当成Rails的model类使用

Posted by liubin on Thursday March 27 @ 11:15
Taged with: tagged , and

LuRuJu是Lube between Ruby and JUDE的缩写,它的目的就是无缝链接ror程序和jude的模型
特点包括:
1.配置简单,只需要把jude文件放到RAILS_ROOT/app/model下即可。Jude模型会作为mvc的m来被使用。目前实现了类之间的1对1,1对n关联。也支持类之间的继承关系。
2.不需要再维护migrate文件,只需运行
lor:db:create就可以了。表的定义使用jude文件中的定义。
3.可以往model里追加Ruby代码来扩种业务逻辑等
4.和config.cache_classes联动,不需要重启rails就可以修改jude文件并使之有效。
5.即可以用jruby,也可以用cruby

这个软件的作者是野村周平,文档 多是日文,不过看代码应该能看的差不多。
主页是 http://luruju.com/


用Ruby抓取网页通过ActiveRecord存到数据库

Posted by liubin on Friday March 7 @ 9:45
Taged with: tagged , , , and

用Ruby抓取网页通过ActiveRecord存到数据库

工作内容:抓取指定网页的规则的连接地址的内容,并取得其网页中指定部分的内容存到数据库。

技术点1:抓取并分析网页
采用hpricot,其主页可见(http://code.whytheluckystiff.net/hpricot/)。Hpricot is a very flexible HTML parser。
doc = Hpricot(open(url))
这个就是去的url的内容,返回一个doc,这个是Element类的实例对象。也可以认为是一个DOM。
可以对这个对象进行查找,修改等操作,具体见hpricot主页的文档。
技术点2:编码转换
网页可能为gbk编码,也可能为gb2312编码。
数据库为mysql,UTF-8编码,以防万一,在建立数据库连接之后,执行下列语句:
ActiveRecord::Base.connection.execute ‘SET NAMES UTF8′
把mysql客户端的编码也设为UTF8,以防出现乱码。
然后用Iconv转换抓取的数据:

ar_object.some_text = Iconv.new(”UTF-8//IGNORE”,”gb2312″).iconv(gb2312_data )
这个就是把gb2312的数据转换为utf-8了。
另外,不知道不进行上面转换,而直接实用gb2312编码的数据,在建立数据库客户端连接之后,执行
ActiveRecord::Base.connection.execute ‘SET NAMES GB2312′
是不是也可以呢,没试过。


RubyConf 2007 agenda

Posted by liubin on Friday September 7 @ 8:24
Taged with: tagged

RubyConf 2007
November 2-4, 2007
Charlotte, North Carolina
http://www.rubyconf.org/agenda.html

TAG:

Ruby也要搞认证考试了

Posted by liubin on Thursday August 30 @ 19:50
Taged with: tagged

今年10月在东京和Matz在住的松江,明年2月开始开始网络版的考试,有英语和日语。
Matz和他们公司的另两个人组成了一个Ruby Association LLC (http://www.ruby-assn.org/index.html.en),当然,并没有辞职,这个公司是个LLC,主要为了帮助Ruby发展而建立的。这个认证也是由Ruby Association主办的。

TAG:

IronRuby 和 Ruby.NET的不同点

Posted by liubin on Sunday July 29 @ 11:24
Taged with: tagged , and

参见http://article.gmane.org/gmane.comp.lang.ruby.dotnet/57

CLR(Common Language Runtime)和DLR(Dynamic Language Runtime)
* IronRuby 基于DLR.
* Ruby.NET 基于 CLR.

与Silverlight的集成
*IronRuby最初的alpha版将不会支持Silverlight,将来版本会考虑这个问题。
*Ruby.NET不清楚

* IronRuby使用了Ruby.NET的分析器,这个分析器基于MSFT许可。

对Ruby库的支持
* Ruby.NET完全支持1.8.2核心库。
* IronRuby支持的比较有限。


百度的一道题

Posted by liubin on Friday June 29 @ 22:59
Taged with: tagged

题目:有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

用Ruby来解,不知道结果对不对。

  1. #define the Ant class
  2. class Ant
  3.   attr_accessor :direction,:pos,:id
  4.  
  5.   def veer # turn back when run into other ants
  6.     @direction = 1 - @direction
  7.   end
  8.  
  9.   def arrived?
  10.      return true if @pos == 0 or @pos == 27
  11.      return false
  12.   end
  13.  
  14.   def step
  15.      if @direction == 1 then
  16.          @pos = @pos + 1
  17.      else
  18.          @pos = @pos - 1
  19.      end
  20.   end
  21.  
  22.   def run_into(a) # if run into other ant,return true if so
  23.       return false if a.direction == self.direction 
  24.       return true  if a.pos == self.pos
  25.       return false
  26.   end
  27.  
  28.   def to_s
  29.      "id:#{@id}.direction:#{@direction}.pos:#{@pos}"
  30.   end
  31.  
  32. end # end of Ant
  33.  
  34. class Hash
  35.    def keys
  36.        a = Array.new
  37.        self.each_key do |k|
  38.          a.push k
  39.        end
  40.        a
  41.    end
  42. end
  43.  
  44. #initialize the ant[5] array
  45. def create_ant(d1,d2,d3,d4,d5)
  46.     a1,a2,a3,a4,a5=nil,nil,nil,nil,nil
  47.     (1..5).each do |i|
  48.        eval " a#{i} = Ant.new;a#{i}.id = #{i};a#{i}.direction = d#{i};"
  49.     end
  50.  
  51.     a1.pos = 3
  52.     a2.pos = 7
  53.     a3.pos = 11
  54.     a4.pos = 17
  55.     a5.pos = 23
  56.     return [a1,a2,a3,a4,a5]
  57.    
  58. end
  59.  
  60. #if all ant has arrived ,return true
  61. def all_arrived a
  62.    a.each do |a1|
  63.       return false unless a1.arrived?
  64.    end
  65.    return true
  66. end
  67.  
  68. # get one plan's cost
  69. # p is for puts the ant's progress if it is set to true
  70. def travel(d1,d2,d3,d4,d5,p=false)
  71.    c = 0 #total cost
  72.    x = 0 #loop variables
  73.    onway = create_ant(d1,d2,d3,d4,d5)
  74.    while true
  75.      break if all_arrived(onway)
  76.      x = 0
  77.      while (x < onway.length-1) do
  78.          (x=x+1 ; next) if onway[x].arrived?
  79.      if onway[x].run_into onway[x+1] then
  80.         onway[x].veer
  81.         onway[x+1].veer
  82.             x = x + 2
  83.      else
  84.         x = x + 1
  85.      end
  86.      end
  87.      onway.each do |ai|
  88.          ai.step unless ai.arrived?
  89.      end
  90.      c = c + 1
  91.      print "   step#{format("%2d",c)} : #{format("%-2d",onway[0].pos)},#{format("%-2d",onway[1].pos)}," if p
  92.      print "#{format("%-2d",onway[2].pos)},#{format("%-2d",onway[3].pos)},#{format("%-2d",onway[4].pos)} \n" if p
  93.  
  94.    end
  95.    c
  96. end
  97.  
  98. #path = {path length=><Array of direction info>}
  99. path = Hash.new
  100.  
  101. [1,0].each do |d1|
  102. [1,0].each do |d2|
  103. [1,0].each do |d3|
  104. [1,0].each do |d4|
  105. [1,0].each do |d5|
  106.     cost = travel(d1,d2,d3,d4,d5)
  107.     (path[cost]||=Array.new).push binding
  108. end
  109. end
  110. end
  111. end
  112. end
  113.  
  114. puts "short path length : #{path.keys.sort.first},path count :#{path[path.keys.sort.first].length}"
  115.  
  116. path[path.keys.sort.first].each_with_index do |b,i|
  117.     puts " short path  #{format("%2d",i+1)}:"
  118.     eval " print '   direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
  119.     #eval "travel(d1,d2,d3,d4,d5,true)", b
  120. end
  121.  
  122. puts "long path length : #{path.keys.sort.last},path count :#{path[path.keys.sort.last].length}"
  123.  
  124. path[path.keys.sort.last].each_with_index do |b,i|
  125.     puts " long path #{format("%2d",i+1)}:"
  126.     eval " print '   direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
  127.     #eval "travel(d1,d2,d3,d4,d5,true)", b
  128. end
TAG:

binding

Posted by liubin on Friday June 22 @ 19:47
Taged with: tagged

今天看Ruby Cookbook发现了这里面对binding的解释是目前看到的比较通俗易懂的了。
A Binding object is a bookmark of the Ruby interpreter’s state. It tracks the values of any local variables you have defined, whether you are inside a class or method definition, and so on.

Once you have a Binding object, you can pass it into eval to run code in the same context as when you created the Binding. All the local variables you had back then will be available. If you called Kernel#binding within a class definition, you’ll also be able to define new methods of that class, and set class and instance variables.

binding方法会返回一个Binding对象的实例,它记载了创建它的时候的环境,比如当时的变量等。然后你可以把这个binding变量传给方法等,它经常作为eval的第二个参数,这样eval里面就不会因为找不到变量出错了。
(more…)

TAG:

用bit-struct处理结构化(二进制)数据

Posted by liubin on Saturday June 16 @ 19:59
Taged with: tagged

这次测试需要建立很多数据文件(record)导入到数据库了。
导入的时候要求数字是内部的二进制形式,Ruby中没有像c一样处理以字节为单位的类型,把整数写到文件里,不小心就会变成字符串了,比如想写1,就会写进去一个1这个字符串(内部表示0×31),而不是写进去0×01。
有了这个bit-struct,就可以方便的处理这个问题了。
bit-struct是用string来存贮各种二进制数据,支持有无符号的整数,字符串,float等,应该足够用了。

这是一个例子:

  1. require 'bit-struct'
  2.  
  3. class C < BitStruct
  4.   signed      :id, 32
  5.   char        :name,160
  6.   signed      :age,16
  7.   unsigned    :dept,16
  8. end
  9.  
  10. File.delete("C:\\Program Files\\Crystal Point\\OutsideView32\\Upload\\aaa")
  11. f=open("C:\\Program Files\\Crystal Point\\OutsideView32\\Upload\\aaa","wb")
  12.  
  13. 5.times do |i|
  14.  
  15.   c = C.new
  16.   c.id = 2009 + i
  17.   c.name = ((i % 10 ).to_s) *20
  18.   c.age = -2 * i
  19.   c.dept= 3 * i
  20.   f.write(c)
  21. end
  22.  
  23. f.close

(more…)

TAG:

【翻译】YARV源码读解(2)

Posted by liubin on Friday June 15 @ 19:22
Taged with: tagged and

YARV源码读解(2)
原文:http://d.hatena.ne.jp/hzkr/20061103
第二回了,上回看了ruby命令启动到yarv的评价器入口:

  1. VALUE iseq = th_compile_from_node(GET_THREAD(), node, file);
  2. return yarvcore_eval_iseq(iseq);

这次,来看一下这个函数的前半部分,进入到th_compile_from_node里面去。在这之前,先看看大体的流程和一些数据的构造。
编译处理流程

(more…)

TAG:

Dave Thomas 在日本Rubykaigi2007上的演讲

Posted by liubin on Friday June 15 @ 19:05
Taged with: tagged and

可惜没有QA的那部分。
语速还是比较慢的。
当然是英语。
RubyKaigi2007 / Dave Thomas 1/4

(more…)



Locations of visitors to this page


Recent Comments