ruby语法-传值

=begin
def test1(*a)
  p a.first
  p a.last
  p a.length
end
test1(3,6)
#>>
#3
#6
#2
<<
=end

=begin
def test2(a,*b)
  p a
  p b
  p b.length
  p b.first
end
test2(1)
test2(1,:a => 1, :b => 2)
#>>
#1
#[]
#0
#nil
#1
#[{:a=>1, :b=>2}]
#1
#{:a=>1, :b=>2}
#<<
=end

=begin
def test3(a,b,*c)
  p a
  p b
  p b.length
  p c
  p c.length
end
test3(1,:a => 1, :b => 2)
#>>
#1
#{:a=>1, :b=>2}
#2
#[]
#0
#<<
=end

=begin
def test3(a,b,c={})
  p a
  p b
  p b.length
  p c
  p c.length
end
test3(1,:a => 1, :b => 2)
#>>
#1
#{:a=>1, :b=>2}
#2
#{}
#0
#<<
=end

=begin
def test3(a,b,c)
  p a
  p b
  p b.length
  p c
  p c.length
end
test3(1,:a => 1, :b => 2)
#>>
#test.rb:81:in `test3': wrong number of arguments (2 for 3) (ArgumentError)
# from test.rb:81
#<<
=end


=begin
def test4(a,b)
  p a
  p b
end
test4(*[2,3])
#>>
#2
#3
#<<
=end


=begin
def a(a, *b, &block)
  p a
  p b
  block.call
end

a(3,4) {puts "aaa"}
#>>
#3
#4
#aaa
#<<
=end

=begin
def a(a, *b, &block)
  p a
  p b
  block.call
end
a(3) {puts "aaa"}
#>>
#3
#[]
#aaa
#<<
=end


#def link_to(name,options={},html_options=nil)
#  p name
#  p options
#end 

#def link_to_tag(name,options={},html_options=nil,*parameters_for_method_reference)
#link_to(name,options,html_options=nil,*parameters_for_method_reference)
#end
#link_to_tag("submit","http://www.sina.com",{:class=>"button"},["a","b"])


class A
   @cached_settings = {}
  def self.[](name)
    v = @cached_settings[name]
    v ? v : (@cached_settings[name] = rand(10))
  end
end

p A["a"]
#p A["a"]
#p A["a"]












猜你喜欢

转载自foyoto.iteye.com/blog/1156144