ruby 正则表达式中的全局变量

在使用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 正则表达式脱字符

re = /[^aeiou]/ # Match any character except a, e, i, o, u

ruby 正则表达式获取匹配字符串前后部分

ruby 正则表达式获取匹配字符串前后部分 a = "Hello gagahappy Hi" refs = a.match(/gagahappy/) refs.pre_match # => "Hello " refs.post_match # => " Hi"

ubuntu 11.10 rvm 安装版ruby openssl解决

用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

在用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

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

# 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

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

require ‘cgi’ url = ‘http://www.gagahappy.com/美食大下’ url_escape = CGI::escape(url) url_unescape = CGI::unescape(url_escape) puts url_escape puts url_unescape

如何理解ruby中的__FILE__?

require File.expand_path(__FILE__ + ‘/../filename’)

Powered by WordPress | Find Wireless Deals at BestInCellPhones.com. | Thanks to iCellPhoneDeals.com Free Cell Phones, Find Highest CD Rates and Incinerador De Grasa Revisión