在使用sub,gsub 时,正则表达式不中不能使用全局变量 str = "a123b456c789" str.sub(/(a\d+)(b\d+)(c\d+)/,"a=#{$1}, b=#{$2}, c=#{$3}") => "a=, b=, c=" 可以使用 str = "a123b456c789" str.sub(/(a\d+)(b\d+)(c\d+)/,’a=\1, b=\2, c=\3′) => "a=123, b=456, c=789" 或 str = "a123b456c789" str.sub(/(a\d+)(b\d+)(c\d+)/) {"a=#{$1}, b=#{$2}, c=#{$3}"} => "a=123, b=456, c=789"
ruby 正则表达式脱字符
February 15th, 2012
moonfox re = /[^aeiou]/ # Match any character except a, e, i, o, u
ruby 正则表达式获取匹配字符串前后部分
February 13th, 2012
moonfox ruby 正则表达式获取匹配字符串前后部分 a = "Hello gagahappy Hi" refs = a.match(/gagahappy/) refs.pre_match # => "Hello " refs.post_match # => " Hi"
ubuntu 11.10 rvm 安装版ruby openssl解决
February 12th, 2012
moonfox 用rvm 安装ruby1.9.1时,需要openssl的版本为0.98,而系统自带为1.0,造成RUNERR,解决方法如下 rvm pkg install openssl rvm remove 1.9.2 rvm install 1.9.2 –with-openssl-dir=$rvm_path/usr
Error loading gem paths on load path in gem_prelude
February 11th, 2012
moonfox 在用rvm安装ruby后运行gem,出现 Error loading gem paths on load path in gem_prelude can’t modify frozen string :69:in `force_encoding’ :69:in `set_home’ :38:in `dir’ :76:in `set_paths’ :47:in `path’ :286:in `push_all_highest_version_gems_on_load_path’ :355:in `‘ 解决方法:找到 your/home/.rvm/src/ruby-your-version/gem_prelude.rb文件 编辑 def self.set_home(home) home = home.gsub File::ALT_SEPARATOR, File::SEPARATOR if File::ALT_SEPARATOR – @gem_home = home.force_encoding(Encoding.find(’filesystem’)) + @gem_home = home.dup.force_encoding(Encoding.find(’filesystem’) end
Rack::Builder rackup
February 9th, 2012
moonfox rackup converts the supplied rack config file to an instance of Rack::Builder. This is how is happens under the hood ( just so you get an idea ) : config_file = File.read(config) rack_application = eval("Rack::Builder.new { #{config_file} }") And then rackup supplies rack_application to the respective webserver : server.run rack_application, options Very straight forward! In [...]
Eager load application classes
February 8th, 2012
moonfox # Eager load application classes def load_application_classes return if $rails_rake_task if configuration.cache_classes configuration.eager_load_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| require_dependency file.sub(matcher, ‘\1′) end end end end
rails Rails::OrderedOptions
February 7th, 2012
moonfox class Rails::OrderedOptions < Array #:nodoc: def []=(key, value) key = key.to_sym if pair = find_pair(key) pair.pop pair << value else self << [key, value] end end def [](key) pair = find_pair(key.to_sym) pair ? pair.last : nil end def method_missing(name, *args) if name.to_s =~ /(.*)=$/ self[$1.to_sym] = args.first else self[name] end end [...]
ruby escape
February 6th, 2012
moonfox require ‘cgi’ url = ‘http://www.gagahappy.com/美食大下’ url_escape = CGI::escape(url) url_unescape = CGI::unescape(url_escape) puts url_escape puts url_unescape
RSS Feed
Twitter
Posted in
Tags: