Ruby 语言 思想驱动生活

August 11, 2007

用批处理(bat)文件切换ip地址

Filed under: Ruby — liubin @ 8:09

因为要连好几个server,因为各种原因,每个server都是在分离的网络中,不能互相访问,必须切换不同的hub,换ip
从网络属性用鼠标来改比较麻烦,所以写个bat文件来执行,只需要双击就行了。

  1. @echo off
  2. set eth="本地连接1"
  3.  
  4. set ip=192.168.1.3
  5. set gw=192.168.1.1
  6. set netmasks=255.255.255.248
  7. echo change ip to %ip%
  8.  
  9. netsh interface ip set address %eth% static %ip% %netmasks% %gw% 1
  10.  
  11. rem netsh interface ip set address %eth% source=dhcp
  12. pause
  13. close

netsh interface ip 命令跟ifconfig类似。

July 29, 2007

IronRuby 和 Ruby.NET的不同点

Filed under: Ruby — liubin @ 11:24

参见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支持的比较有限。

June 29, 2007

百度的一道题

Filed under: Ruby — liubin @ 22:59

题目:有一根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

June 22, 2007

binding

Filed under: Ruby — liubin @ 19:47

今天看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…)

June 16, 2007

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

Filed under: Ruby — liubin @ 19:59

这次测试需要建立很多数据文件(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…)

June 15, 2007

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

Filed under: Ruby — liubin @ 19:22

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…)

Dave Thomas 在日本Rubykaigi2007上的演讲

Filed under: Ruby — liubin @ 19:05

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

(more…)

June 13, 2007

你真二

Filed under: Ruby,垃圾 — liubin @ 23:44

现在已经不是骂人你的话了。
二就是2.0了,升级了。
另,matz还真忙,6月9号当天就不知道坐飞机去哪里了,不过那天的slide已经放到网上了
http://www.rubyist.net/~matz/slides/rk2007-matz/
http://www.rubyist.net/~matz/slides/rk2007-matz/mgp00007.html
from java to ruby,很多人都引用了这个封面图片。

平锅健儿关于Ruby的Agile开发的演讲

Filed under: Ruby — liubin @ 23:18

最近大家知道平锅san也许是因为最近流传着他和Matz等三人的关于Ruby和Agile开发的视频。
其实,平锅健儿是change-vision的CEO吧。著名的UML建模工具Jude就是他们公司的。平锅还是永和系统管理株式会社的副总。上面说道的那个视频可以在http://jude-users.com/en/modules/weblog/details.php?blog_id=28找到,那个角谷信太郎是他的手下。但是在那天的演讲中说他是角谷的上司的上司的上司,玩笑?角谷翻译了《From Java to Ruby》。

平锅是在LT(Lightning Talks)时段发表的,这个时间段每个人只有5分钟的时间,主屏幕的旁边的屏幕有倒计时,还剩1分钟时候会响一下铃,倒计时屏幕变红。5分满的时候响锣。
平锅的主题是Software is Made of Design(Idea) and Communication
视频可以看http://www.youtube.com/watch?v=sufSoCEhp5E
主要讲了idea和交流的是比较重要di,Ruby+Rails+Agile是OPEN的。

另外还有一些视频,可以自己搜一下,不过我看了一下都不是很清楚,而且都是日语的,没什么好看的。
另,http://jude-users.com/en/modules/weblog/可以去看看,英文blog。

Japan RubyKaigi2007的一些图片

Filed under: Ruby — liubin @ 22:00

I download some of this picture from http://www.flickr.com
because for some reason china user can not access flickr for some days
发的书包

539392850_e7b2f65c53.jpg

女主持人,不知道从哪里借来的。

542469227_3cda84b7f2.jpg

Charles Nutter / Thomas Enebo 在讲JRuby。

537848044_5405ee8eb2.jpg
(more…)

« Newer PostsOlder Posts »

Powered by WordPress