环境:
tor + windows
toonel.jar + linux
最近做了个自已用的 浏览器的 bookmarklet,可以一键 同步更新自己的 status(twitter,douban,xiaonei,kaixin001,meme等),这里以twitter为例,通过twitter api + 代理 更新自己的 tweets
1,windows 平台
(1)打开自己的tor 代理
(2)核心代码
def twitter email = '你注册的twitter email' password = '你的twitter密码' update_text = 'test status from twitter api by proxy' proxy_host = '127.0.0.1' # tor proxy_port = 8118 # tor 的http 端口是 8118 # toonel # proxy_port = 8080 proxy = Net::HTTP::Proxy(proxy_host, proxy_port) url = URI.parse('http://twitter.com/statuses/update.xml') proxy.start(url.host,url.port) do |h| # Create the POST request req = Net::HTTP::Post.new(url.path) req.basic_auth email, password req.set_form_data({'status' => update_text}) response = h.request(req) # Check the request's response if response.message == 'OK' puts response.body else puts 'failure' end end end
2,linux平台,下面这种方法windows同样适用,java是跨平台的
(1)打开 toonel.jar 代理
(2)把上面 的 port 改为 proxy_port = 8080
为了 blog的 安全 , 这里没有介绍 代理的方法,请自行google,当然代理的办法有很多,更新twitter的tweets 也有很多办法,可以根据自己的代理,自行修改代码~!
环境 : ruby 1.9.2 + ruby 1.8.7
ruby 1.8 时代 char to ascill 一般这么做:
"a"[0] # 97 ?a # 97
但是 1.9 时代 该方法 不可以了,输出如下:
"a"[0] # a ?a # a
后来 我用了 一个 变态的 方法:
"a".bytes.to_a.first # 97
1.9 时代 应该使用ord 函数
"a".ord # 97 "a"[0].ord # 97 ?a.ord # 97
ascill to char 通用:
97.chr # "a"
但是 ord 函数 1.8 环境下 同样可以使用;
看下 ord 函数的源码(built in 方法,内部看不到ruby代码):
# int.ord => int # # # Returns the int itself. # # ?a.ord #=> 97 # # This method is intended for compatibility to # character constant in Ruby 1.9. # For example, ?a.ord returns 97 both in 1.8 and 1.9. # # def ord # This is just a stub for a builtin Ruby method. # See the top of this file for more info. end
see:
http://stackoverflow.com/questions/1270209/getting-an-ascii-character-code-in-ruby-fails
环境:ruby 1.9.2 + ubuntu 10.10 + rails 3.0.3
我们知道 ruby 中 扩展class ,写公用方法 ,或者 利用命名空间 来模块化 ,都是通过 module 来实现的 , 今天 看 rails 中 camelize 方法的源码的时候 , 发现 module 这样写的…..
active_support/inflector/methods.rb
module ActiveSupport # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept # in inflections.rb. # # The Rails core team has stated patches for the inflections library will not be accepted # in order to avoid breaking legacy applications which may be relying on errant inflections. # If you discover an incorrect inflection and require it for your application, you'll need # to correct it yourself (explained below). module Inflector extend self # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase. # # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces. # # Examples: # "active_record".camelize # => "ActiveRecord" # "active_record".camelize(:lower) # => "activeRecord" # "active_record/errors".camelize # => "ActiveRecord::Errors" # "active_record/errors".camelize(:lower) # => "activeRecord::Errors" # # As a rule of thumb you can think of +camelize+ as the inverse of +underscore+, # though there are cases where that does not hold: # # "SSLError".underscore.camelize # => "SslError" def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end # Makes an underscored, lowercase form from the expression in the string. # # Changes '::' to '/' to convert namespaces to paths. # # Examples: # "ActiveRecord".underscore # => "active_record" # "ActiveRecord::Errors".underscore # => active_record/errors # # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+, # though there are cases where that does not hold: # # "SSLError".underscore.camelize # => "SslError" def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end # Replaces underscores with dashes in the string. # # Example: # "puni_puni" # => "puni-puni" def dasherize(underscored_word) underscored_word.gsub(/_/, '-') end # Removes the module part from the expression in the string. # # Examples: # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections" # "Inflections".demodulize # => "Inflections" def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end # Creates a foreign key name from a class name. # +separate_class_name_and_id_with_underscore+ sets whether # the method should put '_' between the name and 'id'. # # Examples: # "Message".foreign_key # => "message_id" # "Message".foreign_key(false) # => "messageid" # "Admin::Post".foreign_key # => "post_id" def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") end # Ruby 1.9 introduces an inherit argument for Module#const_get and # #const_defined? and changes their default behavior. if Module.method(:const_get).arity == 1 # Tries to find a constant with the name specified in the argument string: # # "Module".constantize # => Module # "Test::Unit".constantize # => Test::Unit # # The name is assumed to be the one of a top-level constant, no matter whether # it starts with "::" or not. No lexical context is taken into account: # # C = 'outside' # module M # C = 'inside' # C # => 'inside' # "C".constantize # => 'outside', same as ::C # end # # NameError is raised when the name is not in CamelCase or the constant is # unknown. def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end else def constantize(camel_cased_word) #:nodoc: names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name) end constant end end # Turns a number into an ordinal string used to denote the position in an # ordered sequence such as 1st, 2nd, 3rd, 4th. # # Examples: # ordinalize(1) # => "1st" # ordinalize(2) # => "2nd" # ordinalize(1002) # => "1002nd" # ordinalize(1003) # => "1003rd" def ordinalize(number) if (11..13).include?(number.to_i % 100) "#{number}th" else case number.to_i % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" else "#{number}th" end end end end end
发现 了这样的写法
module A module B extend self ..... end end
extend self 有何作用?
先来看个demo:
module Foo module Bar extend self # self => Foo::Bar def hello p "hello" end end end class Klass include Foo::Bar end Klass.new.hello # "hello" Foo::Bar.hello # "hello" Klass.hello # undefined method `hello' for Klass:Class (NoMethodError)
发现 module 中的方法 可以当作模块方法 直接被Module调用 , 被include 到class 中后 , 依然还是 class 的实例方法 , 恩,不错,以后 像下面 这样的写法, 都要 改改了:
module A def self.foo end end
改成这样:
module A extend self def foo end end
demo里的extend self 其实就是 Foo::Bar.extend(Foo::Bar)
所以 可以 更动态的 写成这样:
module Foo module Bar def hello p "hello" end end end class Klass include Foo::Bar end Foo::Bar.extend(Foo::Bar) Klass.new.hello # "hello" Foo::Bar.hello # "hello" Klass.hello # undefined method `hello' for Klass:Class (NoMethodError)
环境:ruby 1.9
\1 和 $1 在用 ruby 正则的时候 经常会用到 , 那么有什么区别呢,今天 来梳理一下:
\1 : 是 向后引用 , 常使用在 sub , gsub 中
$1 : 是 ruby 里的全局变量
看几个demo:
demo:
"ab12cd12".gsub(/(\d+)cd(\1)/,"") # => "ab" 这个正则就相当于 /(\d+)cd12/ ,因为 \1 引用的是 前面的 (\d+) ,而前面的 (\d+) 匹配出来的结果是 12 "ab12cd".gsub(/(\d+)/,'34\1') # => "ab3412cd"
p "ab12cd".gsub(/(\d+)/,'34\1') # "ab3412cd" p $1 # "12" p "ab56cd".gsub(/(\d+)/,"78#{$1}") # "ab7812cd" , 这个时候的 $1 为 上面的 12
p "ab12cd".gsub(/(\d+)/,'34\1') # "ab3412cd" p $1 # "12" str = "ab56cd".gsub(/(\d+)/) do |ele| "78#{$1}" # 这里的 $1 是 56 end p str # "ab7856cd"
p "ab56cd".gsub(/(\d+)/,"78#{$1}") # "ab78cd" 这里的 $1 是 nil
str = "ab56cd".gsub(/(\d+)/) do |ele| "78#{$1}" end p str # "ab7856cd"
得出结论:
1,\1 和 $1 是两个 不同的用法
2,特别注意 $1 在 gsub中 block 中,和 写在replacement 中 是不一样的 , \1 用在 replacement 中 ,$1 用在 block 中 ,这个源码中已经说明了
3,\1 必须用单引号
看下源码中的解释:
# str.gsub(pattern, replacement) => new_str # str.gsub(pattern) {|match| block } => new_str # # # Returns a copy of <i>str</i> with <em>all</em> occurrences of <i>pattern</i> # replaced with either <i>replacement</i> or the value of the block. The # <i>pattern</i> will typically be a <code>Regexp</code>; if it is a # <code>String</code> then no regular expression metacharacters will be # interpreted (that is <code>/\d/</code> will match a digit, but # <code>'\d'</code> will match a backslash followed by a 'd'). # # If a string is used as the replacement, special variables from the match # (such as <code>$&</code> and <code>$1</code>) cannot be substituted into it, # as substitution into the string occurs before the pattern match # starts. However, the sequences <code>\1</code>, <code>\2</code>, and so on # may be used to interpolate successive groups in the match. # # In the block form, the current match string is passed in as a parameter, and # variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>, # <code>$&</code>, and <code>$'</code> will be set appropriately. The value # returned by the block will be substituted for the match on each call. # # The result inherits any tainting in the original string or any supplied # replacement string. # # "hello".gsub(/[aeiou]/, '*') #=> "h*ll*" # "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>" # "hello".gsub(/./) {|s| s[0].to_s + ' '} #=> "104 101 108 108 111 " # # def gsub(pattern, replacement) # This is just a stub for a builtin Ruby method. # See the top of this file for more info. end
replacement 时:
# If a string is used as the replacement, special variables from the match
# (such as <code>$&</code> and <code>$1</code>) cannot be substituted into it,
# as substitution into the string occurs before the pattern match
# starts. However, the sequences <code>\1</code>, <code>\2</code>, and so on
# may be used to interpolate successive groups in the match.
block 时:
# In the block form, the current match string is passed in as a parameter, and
# variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
# <code>$&</code>, and <code>$’</code> will be set appropriately. The value
# returned by the block will be substituted for the match on each call.