%w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
button btn, :width => 46, :height => 46 do
method = case btn
when /[0-9]/: 'press_'+btn
when 'Clr': 'press_clear'
when '=': 'press_equals'
when '+': 'press_add'
when '-': 'press_sub'
when '*': 'press_times'
when '/': 'press_div'
end
number.send(method)
number_field.replace strong(number)
end
end
case foo
when /baz/
do_something_with_the_string_matching_baz
when 12..15
do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
# only works in Ruby 1.9 or if you alias Proc#call as Proc#===
do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
do_something_with_the_instance_of_Bar
when some_object
do_something_with_the_thing_that_matches_some_object
end
def multiple_of(factor)
Proc.new{|product| product.modulo(factor).zero?}
end
case number
when multiple_of(3)
puts "Multiple of 3"
when multiple_of(7)
puts "Multiple of 7"
end
module M
def not!
'not!'
end
module_function :not!
end
class C
include M
def fun
not!
end
end
M.not! # => 'not!
C.new.fun # => 'not!'
C.new.not! # => NoMethodError: private method `not!' called for #<C:0x1261a00>
如果在使用 Module _ function时没有任何参数,那么在 module _ function 语句之后的任何模块方法都将自动变成 module _ function 本身。
module M
module_function
def not!
'not!'
end
def yea!
'yea!'
end
end
class C
include M
def fun
not! + ' ' + yea!
end
end
M.not! # => 'not!'
M.yea! # => 'yea!'
C.new.fun # => 'not! yea!'
class A
private
def my_private_method
puts 'private method called'
end
end
a = A.new
a.my_private_method # Raises exception saying private method was called
a.send :my_private_method # Calls my_private_method and prints private method called'
- n 设置一个只有“ gets”的外部循环-它神奇地对给定的文件名或 STDIN 起作用,在 $_ 中设置每个读行
- p 类似于-n,但在每次循环迭代结束时使用自动 put
在每个输入线上自动调用。分割,存储在 $F 中
- i 就地编辑输入文件
- l 自动呼叫。咀嚼输入
执行一段代码
检查源代码
有警告
一些例子:
# Print each line with its number:
ruby -ne 'print($., ": ", $_)' < /etc/irbrc
# Print each line reversed:
ruby -lne 'puts $_.reverse' < /etc/irbrc
# Print the second column from an input CSV (dumb - no balanced quote support etc):
ruby -F, -ane 'puts $F[1]' < /etc/irbrc
# Print lines that contain "eat"
ruby -ne 'puts $_ if /eat/i' < /etc/irbrc
# Same as above:
ruby -pe 'next unless /eat/i' < /etc/irbrc
# Pass-through (like cat, but with possible line-end munging):
ruby -p -e '' < /etc/irbrc
# Uppercase all input:
ruby -p -e '$_.upcase!' < /etc/irbrc
# Same as above, but actually write to the input file, and make a backup first with extension .bak - Notice that inplace edit REQUIRES input files, not an input STDIN:
ruby -i.bak -p -e '$_.upcase!' /etc/irbrc
随时谷歌“红宝石一行程序”和“ perl 一行程序”吨更有用和实际的例子。它实际上允许您使用 ruby 作为 awk 和 sed 的一个相当强大的替代品。
require 'active_support'
days = 5.days
days.class #=> Fixnum
days.is_a?(Fixnum) #=> true
Fixnum === days #=> false (huh? what are you really?)
Object.instance_method(:class).bind(days).call #=> ActiveSupport::Duration (aha!)
ActiveSupport::Duration === days #=> true
当然,上面所说的依赖于 active _ support 没有重新定义 Object # instance _ method 这个事实,如果是这样的话,我们就真的遇到麻烦了。不过,我们总是可以在加载任何第三方库之前保存 Object.instance _ method (: class)的返回值。
module Mod
def var_add(more); @var+more; end
end
class Cla
include Mod
def initialize(var); @var=var; end
# override
def var_add(more); @var+more+more; end
end
cla = Cla.new('abcdef')
cla.var_add('ghi') #=> "abcdefghighi"
Mod.instance_method(:var_add).bind(cla).call('ghi') #=> "abcdefghi"
这甚至适用于重写对象所属类的实例方法的单例方法。
class Foo
def mymethod; 'original'; end
end
foo = Foo.new
foo.mymethod #=> 'original'
def foo.mymethod; 'singleton'; end
foo.mymethod #=> 'singleton'
Foo.instance_method(:mymethod).bind(foo).call #=> 'original'
# You can also call #instance method on singleton classes:
class << foo; self; end.instance_method(:mymethod).bind(foo).call #=> 'singleton'
class MathWiz
def add(a,b)
return a+b
end
def method_missing(name, *args)
puts "I don't know the method #{name}"
end
end
mathwiz = MathWiz.new
puts mathwiz.add(1,4)
puts mathwiz.subtract(4,2)