易截截图软件、单文件、免安装、纯绿色、仅160KB

写的一个inter类模仿ruby整数的行为

我们知道ruby中对于整数的[],[]=,<<,>>操作是针对于二进制的值来运算的。
我现在写一个针对十进制数操作的类,拥有整数的所有方法,如下:
class InterEx
def initialize(val=0)
@val=val
end

def to_s
@val.to_s
end

def [](idx)
self.to_s[idx].to_i
end

def []=(idx,val)
s=self.to_s
s[idx] = val.to_s
@val=s.to_i
end

def coerce(other)
[other,@val]
end

def <<(v)
return InterEx.new(@val) if v==0
InterEx.new(@val*10**v)
end

def >>(v)
return InterEx.new(@val) if v==0
s=self.to_s
s=s[0..(-1*v-1)]
InterEx.new(s.to_i)
end

def method_missing(mtd,*args)
InterEx.new(@val.send mtd,*args)
end

end
ruby还提供了delegate机制便于代理一个类,比如要想代理Fixnum类,
可以这么写:
require 'delegate'
class InterEx<DelegateClass(Fixnum)
...
end



相关文档:

ruby命令行解析

   可用库:getoptlong.rs, optionparser
   对应类:GetoptLong,  OptionParser
   前者已过时,建议使用后者,且后者比前者易用。
后者特点:
1. 参数描述和代码处理写在一起
2. 输出参数说明,不需单独维护
3. 可选参数和命令参数描述简洁
4. 参数可自动转换为特定的类
5. ......

Ruby声音


转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......

Windows平台的ruby IDE 点评

转自 http://www.javaeye.com/topic/57474 
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......

ruby类变量在development模式失效

分页中用到类变量,主要是用来标记“页码输入框”的id 如果一个页面有几个分页,“页码输入框”的id要是不同的才能分清是哪个要分页。使用类变量就是为了达到这个目的,让所有的对象实例共用一个变量,不必每次重新初始化变量。 类变量使用代码示例 1 require 'ruby-debug'
2 debugger
3 cla ......

ruby的类与模块(1)

class Point
@x = 1
@y = 2
def initialize(x,y)
@x,@y = x,y
end
end 
代码中的@x,@y为实例变量,实例变量只对self的环境起作用,因此initialize外面的@x=1,@y=2只对类本身起作用,而方法内部,的@x,@y是对对象的实例起作用的。
class Point
include Enumerable
def initialize(x ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号