Region
Dir.glob(regex)
[-1]
optparse 添付のサンプルにある opttest.rb の
42: # boolean switch(default true) 43: @exec = true 44: opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|}ところで、引数クラスは TrueClass ではなくて FalseClass じゃないかと思うのだけど。
FalseClassにしてしまうと、--execのデフォルト値もfalseになってしまうので。
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec
[]
#<Object:0x100f94e8 @exec=true>
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", FalseClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec
[]
#<Object:0x100f94e8 @exec=false>
引数をつけたほうがはっきりするかな?
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec=y
[]
#<Object:0x100f94e8 @exec=true>
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec=n
[]
#<Object:0x100f94e8 @exec=false>
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", FalseClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec=y
[]
#<Object:0x100f94e8 @exec=true>
$ ruby -roptparse -e '@exec=true;p ARGV.options{|opt|opt.on("-n", "--no-exec[=FLAG]", FalseClass, "not really execute") {|@exec|};opt.parse!}, self' -- --exec=n
[]
#<Object:0x100f94e8 @exec=false>
でも1.9だと--exec=yesがエラーになってるな。
Index: lib/optparse.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/optparse.rb,v
retrieving revision 1.54
diff -U2 -p -r1.54 optparse.rb
--- lib/optparse.rb 22 Nov 2005 14:53:11 -0000 1.54
+++ lib/optparse.rb 26 Dec 2005 04:45:23 -0000
@@ -701,5 +701,5 @@ class OptionParser
#
def match(key)
- return key, *fetch(key) {
+ return key, fetch(key) {
raise AmbiguousArgument, catch(:ambiguous) {return complete(key)}
}
あ、なぜかずっと25日だと思い込んでた。 しかしまた土壇場で何もしてないなぁ。 というか、子供できちゃうと本当に時間がない。 まつもとさんはよくできるなぁ、4人もいるのに。
[-1]もともと全部1バイトずらしてあるだけ。
char *
rb_source_filename(const char *f)
{
char *name;
if (!st_lookup(source_filenames, (st_data_t)f, (st_data_t *)&name)) {
long len = strlen(f) + 1;
char *ptr = name = ALLOC_N(char, len + 1);
*ptr++ = 0;
MEMCPY(ptr, f, char, len);
st_add_direct(source_filenames, (st_data_t)ptr, (st_data_t)name);
return ptr;
}
return name + 1;
}
これだとObjManagerをインスタンス化する必要はないような。
それに、ManagedObjを継承しないといけないというのは、(少なくともRubyでは)不必要な制限に思える。
def_featuresを直接Moduleに定義するとか、mix-inにするとかじゃないかなぁ。
通りかかってみれば、なんとじーさんとせきねっちの結婚式がっ!www
Dir.glob(regex)そりゃglobとはいわんのでは。
でも、GNU findにも-regexなんてpredicateがあったし、便利かもなぁ、
と思って久々にman findしてみると-pathは-wholenameに変わったらしい。
関係ないけど。
Dir.open(dir){|d|d.grep(regexp)}
だと、ベース名しか比較しないとか再帰しないとか、ちょっと違うわけだけれど、
Dir.regex(pattern)は、不特定の"/"にマッチしうるパターンを与えると、
常に再帰することになるんだろうか。
でも、うっかりrootからマッチしてしまうパターンを使ってしまったりすると、かなりイヤだな。
クラスメソッドよりも、Dir#globとかDir#findみたいののほうが使いやすいかも。
[ruby-dev:18292]のescaper.rbとか。
Regexp.quoteは全然目的違うので。
RegionEnumerable#uptoはInteger#uptoとかString#uptoでオーバーライドされちゃうからなぁ。
class Region
include Enumerable
attr_reader :begin, :end
def initialize(b, e, opt = nil)
@begin = b
@end = e
@left_exclusive = (opt && /</ =~ opt ? true : false)
@right_exclusive = (opt && />/ =~ opt ? true : false)
end
def left_exclusive?; @left_exclusive end
def right_exclusive?; @right_exclusive end
def each
l = @left_exclusive
r = @right_exclusive
lim = l || r ? -1 : 0
val = @begin
e = @end
return if !e.nil? and (val <=> e) > lim
val = val.succ if l
lim = r ? 0 : 1
begin
yield val
val = val.succ
end while e.nil? or (val <=> e) < lim
end
def include?(val)
if !@begin.nil? and (val <=> @begin) < (@left_exclusive ? 1 : 0)
false
elsif !@end.nil? and (val <=> @end) > (@right_exclusive ? -1 : 0)
false
else
true
end
end
alias === include?
def step(unit = 1, &block)
unit = unit.to_i
raise ArgumentError, "step can't be negative" if unit < 0
raise ArgumentError, "step can't be 0" if unit.zero?
val = @begin
if val.kind_of?(Numeric)
succ = proc {val += unit}
else
succ = proc {unit.times {val = val.succ}}
end
succ[] if @left_exclusive
if @end.nil?
while true
yield val
succ[]
end
else
Range.new(val, @end, @right_exclusive).step(unit, &block)
end
end
end
module Comparable
def between(e, opt = nil)
Region.new(self, e, opt)
end
end
class NilClass
def between(e, opt = nil)
Region.new(self, e, opt)
end
end
いや、betweenを前置きにしちゃヘンか。
_ jmax [やっと分かりました。 opts.on("-n", "--[no-]exec[=FLAG]", TrueClass)..]