Tips – How to write ruby retry

Posted by AllenWei | Posted in RubyOnRails, Tips | Posted on 15-02-2010-05-2008

View Comments




<br
  retry_times = 3
  begin
    # Exception here
  rescue Exception => e
    retry if (retry_times -= 1 ) > 0
  end

Retryable method I wrote:

  # If you want to get a proxy but afraid of proxy failture, you can use this method.
  # If retry times more than retry times in option parameter, will raise a RetryError.
  # * :retry_times - Retry times , Defaults 10
  # * :on - The Exception on which a retry will be performed. Defaults Exception
  # Notice: This method will call block many times, so don't put can't retryable code in it.
  # Example
  # =======
  #    begin
  #      retryable_proxy(:retry_times => 10,:on => Timeout::Error) do |ip,port|
  #         # your code here
  #      end
  #    rescue RetryError
  #      # handle error
  #    end
  #
  def retryable_proxy(options = {},&block)
    opts = {:retry_times => 10,:on => Exception}.merge(options)
    retry_times, try_exception = opts[:retry_times], opts[:on]
    begin
      proxy = Lookup::Models::ProxyList.random
      return block.call(proxy.ip,proxy.port)
    rescue try_exception => e
      retry if (retry_times -= 1) > 0
    end
    raise RetryError
  end


Tips – parse log file tips

Posted by AllenWei | Posted in Tips | Posted on 15-02-2010-05-2008

View Comments

  1. separate variables by “|”
  2. Analyze
field_names = [:level, :time, :pid, :location, :token, :query, :proxy, :action, :cost, :msg]
log_data = Hash[*field_names.zip(parts).flatten]

Then you can easily get all your variables

puts log_data[:level]
puts log_data[:time]
  1. Combined log files analyze
#code in analyzer.rb
ARGF.each_line do |line|
  puts line
end

Then

ruby analyzer.rb log1.log log2.log

Tips – Require all file in a folder

Posted by AllenWei | Posted in RubyOnRails, Tips | Posted on 15-02-2010-05-2008

View Comments

No recursion

  $LOAD_PATH << File.join(File.dirname(__FILE__), '../app/drops')

Recursively

  Dir.glob(File.join(File.dirname(__FILE__), '../app/drops/*.rb')).each {|f| require f }

Tips – Rewrite private method gracefully

Posted by AllenWei | Posted in RubyOnRails, Tips | Posted on 15-02-2010-05-2008

View Comments

class Test
  private
  def words
    puts "origin word"
  end
end

Test.class_eval do
  alias_method :origin_words, :words
 
  def words
    puts "haha"
    origin_words
    puts "haha end"
  end
end

Test.new.send(:words)

Why we can get params values by both symbol and string

Posted by AllenWei | Posted in RubyOnRails | Posted on 15-02-2010-05-2008

View Comments

Today, I met a weird bug converting params to a hash only has symbol keys.

I see there is a method which in ActiveSupport. symbolize_keys!!, convert all keys of hash to symbol.

So I did this:

def some_action
  params.symbolize_keys!
end

But after print params, I found symbolize_keys! doesn’t work. The keys in params still String.

After some googling, I find that class of params is not hash, it is HashWithIndifferentAccess

Then I find symbolize_keys! method just return self

The description of this class is:
bq. This class has dubious semantics and we only have it so that people can write params[:key] instead of params[âkeyâ] and they get the same value for both keys.

I realized that’s why we can get values from params by both Symbol and String

So You can also use this powerful tool:

  h = HashWithIndifferentAccess.new
  h["allen"] = "wei"
  puts h["allen"] # print "wei"
  puts h[:allen]  # print "wei"

You don’t need symbolize_keys!!.

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.