Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

January 7, 2010

How come Regexp.unescape is unavailable?

書いてみた。自分の技量ではこれ以上縮まる気がしない。


> Regexp.escape("\\((\t\n").gsub(/\\([\[\]\{\}\(\)\|\-\*\.\\\?\+\^\$\# fnrt])/){|s|i=$1;i=~/[fnrt]/?eval('"'+"\\#{i}"+'"'):i}
=> "\\((\t\n"
[2009-01-08]: typoがあったので修正

December 8, 2009

prune - IRC protocol library for Ruby

I'm pleased to have first release of prune, IRC protocol library for Ruby now. which contains the IRC proxy daemon as the sample implementation, but works enough as powerful. in this release, strictly speaking, it doesn't have a server implementation in a class yet though, will be available in the future release.

Major features in prune are/will be:
  • Ruby-friendly to deal with the IRC protocol.
  • easy to extend the behaviour
  • the plugin mechanism
  • the reloading configuration support
Also, the features in the proxy daemon are:
  • able to keep multiple servers connection
  • the encoding support per channels.
For more details of this project, please see http://github.com/tagoh/prune. you can download the source code there too.


Enjoy,

July 14, 2009

How to add a feature to spotlight a column search on ActiveScaffold

ActiveScaffold is really nice to build something with Ruby on Rails. that saves my time a lot. though it may requires extra internal knowledges to do something that it doesn't provide. even generally.

So this is just a tips to add a column-based search feature to the ActiveScaffold. you can just patch it out to lib/active_scaffold/actions/search.rb and/or live_search.rb, or add it to your controller where you want to enable this feature.


def do_search
@query = params[:search].to_s.strip rescue ''

unless @query.empty?
tmpq = @query.split(' ')
columns_query = {}
tmpq.delete_if do |x|
retval = false
if x =~ /\A(.*):(.*)\Z/ then
p = $1
v = $2
if !p.nil? && !p.empty? && !v.nil? && !v.empty? &&
active_scaffold_config.columns.include?(p)
then
columns_query[p] = v
retval = true
end
end
retval
end
columns = active_scaffold_config.live_search.columns
like_pattern = active_scaffold_config.live_search.full_text_search? ? '%?%' : '?%'
search_conditions = self.class.create_conditions_for_columns(tmpq, columns, like_pattern)
self.active_scaffold_conditions = merge_conditions(self.active_scaffold_conditions, search_conditions)
columns_query.each do |p,v|
active_scaffold_config.columns.each do |column|
next if column.column.nil?
if column.column.name == p then
sql = column.column.text? ? "LOWER(#{column.search_sql}) LIKE ?" : "#{column.search_sql} = ?"
token = column.column.text? ? like_pattern.sub('?', v.downcase) : column.column.type_cast(v)
self.active_scaffold_conditions = merge_conditions(self.active_scaffold_conditions, [sql, token])
break
end
end
end
@filtered = !search_conditions.blank?

includes_for_search_columns = columns.collect{ |column| column.includes}.flatten.uniq.compact
self.active_scaffold_joins.concat includes_for_search_columns

active_scaffold_config.list.user.page = nil
end
end

October 25, 2007

rdocで生成されるyamlのファイルがへん? (続き)

えらく時間がかかってしまったけど、とりあえず先日の問題の結果だけ。

diff -ruN ruby-1.8.5.orig/lib/rdoc/rdoc.rb ruby-1.8.5/lib/rdoc/rdoc.rb
--- ruby-1.8.5.orig/lib/rdoc/rdoc.rb 2006-08-05 07:00:25.000000000 +0900
+++ ruby-1.8.5/lib/rdoc/rdoc.rb 2007-10-25 18:17:03.000000000 +0900
@@ -136,7 +136,7 @@
result = []

patterns.split.each do |patt|
- candidates = Dir.glob(File.join(in_dir, patt))
+ candidates = Dir.glob(File.join(in_dir, patt)).sort
result.concat(normalized_file_list(options, candidates))
end
result
@@ -184,7 +184,7 @@
# we may well contain subdirectories which must
# be tested for .document files
def list_files_in_directory(dir, options)
- normalized_file_list(options, Dir.glob(File.join(dir, "*")), false, optio
ns.exclude)
+ normalized_file_list(options, Dir.glob(File.join(dir, "*")).sort, false,
options.exclude)
end

こんな感じで、.documentの結果および与えられたパスの結果を一定にすることで、回避。適度に:nodoc:、:startdoc:や:stopdoc:で制御するとあってしかるべきファイルまでなくなる副作用があるぽいので、rdocそのものを大幅に改良しないとだめな気がする。問題の発生する状況からして、あまりそのへん考えられていなかったような気がするけど。

October 18, 2007

rdocで生成されるyamlのファイルがへん?

multilibの都合で、例えばi386とx86_64で同一パス上にインストールされるようなファイルは中身は同じでなければならない、という条件があって、これが違うとrpmはファイルが衝突してると怒るわけだけれども、rdocで生成されるなんちゃら.yamlのファイルでi386とx86_64で結果が違うという問題に遭遇した。

ちなみに結果は以下のとおり:
system/Kernel/warn-i.yaml at i386:
--- !ruby/object:RI::MethodDescription
aliases: []

block_params:
comment:
full_name: Kernel#warn
is_singleton: false
name: warn
params: (msg)
visibility: public


system/Kernel/warn-i.yaml at x86_64:
--- !ruby/object:RI::MethodDescription
aliases: []

block_params:
comment:
- !ruby/struct:SM::Flow::P
body: Display the given message (followed by a newline) on STDERR unless warni
ngs are disabled (for example with the -W0 flag).
full_name: Kernel#warn
is_singleton: false
name: warn
params: |
warn(msg) => nil

visibility: public


ちょっとした一例にすぎないけど、まあこんな感じ。
元となっているerror.c内のコメントをみるとx86_64側が正しい結果に見える。実際のところparse時にはコメントの情報はちゃんと得られているようで、error.c単体をrdocに食わせればそれっぽく出力されるけど、他のメソッドで微妙な抜けや順序が違うなどの問題もでたりするので、ちょっとまだ調査中。