one line log parser

ruby-one-liners

ruby global variables

log文件形如:

method=GET path=/v1/foods/near_by_want1 format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20
method=GET path=/v1/foods/near_by_want2 format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20
method=GET path=/v1/foods/near_by_want3 format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20
method=GET path=/v1/foods/near_by_want  format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20
method=GET path=/v1/foods/near_by_want4 format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20
method=GET path=/v1/foods/near_by_want  format=html controller=api/v1/foods action=near_by_want status=200 duration=2005.69 view=0.20

得到near_by_want的平均处理时间

grep 'near_by_want' production.log-20130827 | awk '{print $7}' | ruby -n -e '@sum ||= 0; $_ =~ /^duration=(.+)/; @sum += $1.to_f; END { puts @sum / $. }'

ruby string percent method

Last week, when i checked a question in StackOverflow, i found an interesting thing. See the example below:

"%d hour %d minutes %d seconds" % [ 1, 20, 30 ]
# => "1 hour 20 minutes 30 seconds"

At first, i thought the percent % is just liked 11%3, so i looked into ruby-doc, i got my answer, % is nothing but a instance method of String

Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string.

"%05d" % 123                              
#=> "00123"

"%-5s: %08x" % [ "ID", self.object_id ]  
#=> "ID: 200e14d6"

"foo = %{foo}" % { :foo => 'bar' }
#=> "foo = bar"

The syntax of a format sequence is follows.

%[flags][width][.precision]type

Kernel#sprintf

ruby private vs protected

ruby中控制访问属性的关键字有三个,publicprotected,和private

public不多说了。来看看protected和private。

我们知道,访问权限关心的一般只有两方面,一个是实例,另一个是继承。

先抛出结论:在ruby中,protected和private都只在类的定义内访问有效,并且可以被继承,但是实例和继承访问都会抛出NoMethodError,另外,两者区别是,protected可以有明确的方法接收者,而private不可以。

注意比较区别,例子如下:

class Foo
  def a
    b
  end
   
  def aa
    c
  end

  def f
     self.b
  end

  def ff
     self.c
  end
protected
  def b
    p  "protected method from Foo"
  end
private
  def c
    p  "private method form Foo"
  end
end

class Bar < Foo
end

Foo.new.b #=> NoMethodError
Foo.new.c #=> NoMethodError
Bar.new.b #=> NoMethodError
Bar.new.c #=> NoMethodError

#======
Foo.new.a    #=> "protected method from Foo"
Foo.new.aa  #=> "private method form Foo"

#======
Foo.new.f #=>  "protected method from Foo"
Foo.new.ff  #=> NoMethodError

ruby class_variables and instance_variables

Ruby 中的类变量和实例变量

例子

class Foo
  @instance_variable = "I am a instance_variable"
  @@class_variable = "I am a class_variable"
 
  def just_output
    p @instance_variable
    p @@class_variable
  end
end

Foo.instance_variables
  #=>   ["@instance_variable"]

Foo.class_variables
  #=>  ["@@class_variable"]

Foo.new.just_output
  #=>  nil
  #=>  "I am a class_variable"

说明:可以看到,实例变量赋初始值都是nil(在初始化 initialization 中除外),而类变量是可以有初始值的。

下面看看作用范围

class Foo
  @instance_variable = "I am a instance_variable"
  @@class_variable = "I am a class_variable"
 
  def change
    @instance_variable = "Changed!"
    @@class_variable = "Changed!"
  end
 
  def just_output
    p @instance_variable
    p @@class_variable
  end
end

foo = Foo.new
foo.change
foo.just_output
  #=>  "Changed!"
  #=>  "Changed!"
# 上面的输出可能看不出区别,继续

bar = Foo.new
bar.just_output
  #=>  nil
  #=>  "Changed!"

说明:实例变量单个实例共享,所以@instance_variable只在 foo 这个实例有值,到 bar 这个实例又是 nil 了。类变量的作用范围是类,所以 foo 和 bar 都是 Foo的实例,都有类变量 @@class_variable 。