Ruby中hash作为函数参数特殊性比较
2014-07-29 01:14
525 查看
a = {1 => 'one',2=>'two'} b = ['three','fore'] def show(x,y) p x p y end
用这段代码来进行试验。
show(2,a)得:
2
{1=>"one", 2=>"two"}
show(2,1 => 'one',2=>'two')得:
2
{1=>"one", 2=>"two"}
show(2,b)得:
2
["three", "fore"]
show(2,'three','fore')得:
1.rb:12:in `show': wrong number of arguments (3 for 2) (ArgumentError)
from 1.rb:12
show(a,2)得:
{1=>"one", 2=>"two"}
2
show(1 => 'one', 2=>'two')得:
1.rb:16:in `show': wrong number of arguments (1 for 2) (ArgumentError)
from 1.rb:16
show(1 => 'one',2=>'two',2)得:
1.rb:15: syntax error, unexpected ')', expecting tASSOC
show(1 => 'one',2=>'two',2)
^
由此可知,当hash作为参数时,只有在最后一个参数的位置传入时不会出错,并且会将若干hash对看成一个元素,所以我们在编程时尽量将hash参数放在最后面。
以上试验在1.8.6版本中进行。
相关文章推荐
- ruby 中hash作为函数参数
- list的排序和比较函数作为参数传递
- ruby **option作为函数参数,map的key必须是符号
- 指针作为函数参数相关问题
- 指针作为函数参数传递
- javascrip 函数作为参数
- 将对象作为参数传递给成员函数
- 利用数组名作为函数参数传递排序和用指针进行排序
- 函数作为参数的传递
- 将一个二维数组intArray[nRow][nCol]作为参数传入上述函数
- 关于用指针作为函数参数申请动态内存
- 指针变量作为函数参数(转贴)
- 任意类成员函数作为线程参数的封装ex
- 线程的封装技巧:把任意类的成员函数指针作为参数
- C语言学习笔记.指针3--指针变量作为函数参数
- Ruby 函数参数多余的空格和括号
- 指针作为函数参数传递 (转载)
- 多维动态数组作为函数传递参数
- 使一个函数作为另外一个函数的参数来运行的javascript代码
- this指针作为函数隐含参数传递的方法