解决ActiveRecord经常要reload的一种方案
Ruby class variable另一奇怪现象的可能的解释

Ruby 1.9.3和Ruby 2.0的arity的对比

bachue posted @ 2013年4月23日 00:25 in Ruby with tags ruby rails proc lambda arity method , 5673 阅读

一直感觉arity这个属性的算法提及的很少(虽然arity是个数学概念,但是简单的数学概念显然无法满足Ruby的复杂的需求),即使在Ruby-doc的MethodProc中也是如此。

Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.

这篇Ruby 2.0的文档里面仅仅提到了几种最简单的情况,还远远算不上复杂,现在我这里列出了比文档里更多的例子,以此来分析arity的行为。

测试代码是这样的:

p proc {}.arity
p proc {||}.arity
p proc {|a|}.arity
p proc {|a,b|}.arity
p proc {|a,b,c|}.arity
p proc {|*a|}.arity
p proc {|a,*b|}.arity
p proc {|a,*b, c|}.arity
p proc {|x = 0, *args|}.arity
p proc {|x=0, y=0, *args|}.arity
p proc {|x, y=0, *args|}.arity
p proc {|(x, y), z=0, *args|}.arity
p proc {|a, b = 1, *c, d|}.arity
p proc {|a = 1, b = 1, *c, d|}.arity

p proc   { |x = 0| }.arity
p lambda { |a = 0| }.arity
def f(a = 0) end; p method(:f).arity
p proc   { |x=0, y| }.arity
p lambda { |x=0, y| }.arity
def f(x = 0, y) end; p method(:f).arity
p proc   { |x=0, y=0| }.arity
p lambda { |x=0, y=0| }.arity
def f(x=0, y=0) end; p method(:f).arity
p proc   { |x, y=0| }.arity
p lambda { |x, y=0| }.arity
def f(x, y=0) end; p method(:f).arity
p proc   { |(x, y), z=0| }.arity
p lambda { |(x, y), z=0| }.arity
def f((x, y), z=0) end; p method(:f).arity

然后是结果对比:

code ruby-1.9.3 ruby-2.0.0
p proc {}.arity 0 0
p proc {||}.arity 0 0
p proc {|a|}.arity 1 1
p proc {|a,b|}.arity 2 2
p proc {|a,b,c|}.arity 3 3
p proc {|*a|}.arity -1 -1
p proc {|a,*b|}.arity -2 -2
p proc {|a,*b, c|}.arity -3 -3
p proc {|x = 0, *args|}.arity -1 -1
p proc {|x=0, y=0, *args|}.arity -1 -1
p proc {|x, y=0, *args|}.arity -2 -2
p proc {|(x, y), z=0, *args|}.arity -2 -2
p proc {|a, b = 1, *c, d|}.arity -3 -3
p proc {|a = 1, b = 1, *c, d|}.arity -2 -2
p proc { |x = 0| }.arity 0 0
p lambda { |a = 0| }.arity 0 -1
def f(a = 0) end; p method(:f).arity -1 -1
p proc { |x=0, y| }.arity 0 -1
p lambda { |x=0, y| }.arity 0 -2
def f(x = 0, y) end; p method(:f).arity -2 -2
p proc { |x=0, y=0| }.arity 0 0
p lambda { |x=0, y=0| }.arity 0 -1
def f(x=0, y=0) end; p method(:f).arity -1 -1
p proc { |x, y=0| }.arity 1 1
p lambda { |x, y=0| }.arity 1 -2
def f(x, y=0) end; p method(:f).arity -2 -2
p proc { |(x, y), z=0| }.arity 1 1
p lambda { |(x, y), z=0| }.arity 1 -2
def f((x, y), z=0) end; p method(:f).arity -2 -2

由这一对比结果,可以清楚的看到:

  • 对于proc而言,如果参数中没有可变长参数(比如*args之类的),则在1.9.3中,arity取决于从左往右没有默认值的参数的数量。2.0.0中取决于整个参数列表中没有默认值的参数数量。
  • 如果有可变长参数,则如果n为整个参数列表中没有默认值的参数数量,arity为-n - 1。
  • 对于lambda而言,可以看到,在1.9.3中情况与proc总是一致的。而在2.0.0中,如果存在有默认值的参数,则如果n为整个参数列表中没有默认值的参数数量,arity = -n -1。否则就等于参数数量。
  • 对于method而言,无论1.9.3还是2.0.0,均与2.0.0中的lambda一致(准确的应该说是lambda在2.0.0中与method保持一致)。
  • 额外的&block不影响到arity的值。

大致结果就是如此了,如果有误,请务必留言告知,谢谢。

لی ویوسوہ ہو یار 说:
2021年5月27日 20:42 Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. know more
لی ویوسوہ ہو یار 说:
2021年6月02日 19:51

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. lifeguard USA

johnyking 说:
2021年6月03日 23:48

I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. demande de visa indien

johnyking 说:
2021年6月04日 01:31

I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks. new zealand visa

لی ویوسوہ ہو یار 说:
2021年6月05日 20:18

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. 메이저사이트

لی ویوسوہ ہو یار 说:
2021年6月07日 17:15

Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive! visit this page

jack peterson 说:
2021年6月10日 20:16

This is a wonderful article,Thank you so much as you have been willing to share information with us.I think this is an informative post.lifeguard training near to me

interstellarconsulti 说:
2021年6月11日 18:00

Its Very Informative Article. Awesome article,We are really grateful for your blog post.https://interstellarconsulting.dk/

johnyking 说:
2021年6月16日 14:56

Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. 먹튀사이트

jackjohnny 说:
2021年6月28日 18:02

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... <a href="https://voyance-telephone-gaia.com/">voyance-telephone-gaia.com</a>

johnyking 说:
2021年7月15日 20:03

Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. scarlett johansson height and weight

Buy Dihydrocodeine o 说:
2021年9月11日 20:18

There are many dissertation websites from the web stay get not surprisingly recognized within the web sites. Buy Dihydrocodeine online

BILAL 说:
2021年10月01日 00:13

You'll find so many dissertation sites on the net when you get relatively observed inside the website. IT Support

BILAL 说:
2021年10月01日 18:22

Types of dissertation internet sites on-line as you are in addition get admittedly believed as part of your site. Computer repairs

BILAL 说:
2021年10月01日 21:53

There are particular dissertation websites on the internet should you acquire certainly introduced in your web page. IT Training

johnyking 说:
2021年12月22日 02:56

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks 토토사이트

TS Jr Inter Previous 说:
2022年8月19日 21:22

The Telangana Inter First Year Important Model Question Paper 2023 is happy to be made available for download to all candidates who will be taking the examinations. Please check the Important Model Question Paper 2023 from the sections below. So, in order to achieve the highest possible score on the test, prepare for it. TS Jr Inter Previous Paper 2023 Additionally, we advise the candidates to download the Subject-specific TS Inter First Year Important Model Question Paper 2023 from the attached direct links at the bottom of the page. For further information, see the sections below. Based on this the Telangana Intermediate I Year Board Important Model Question Paper 2023 is created by the Education Board.

Lifeguard Training 说:
2023年7月06日 14:30

Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks.

jtstephenjack 说:
2023年7月06日 18:10

That's the reason center on it's essential to precise ground moves well before writing. Might be attainable so that you can extra advisable text that way. <a href="https://www.americanlifeguardusa.com/collections/upcoming/products/lifeguard-training/"> Lifeguard training </a>

<a href="https://www 说:
2023年7月06日 18:10

The appearance positively wonderful. All of these miniature info are fashioned utilizing massive amount historical past experience. I want it all significantly

Client management so 说:
2023年7月19日 17:53

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.

jnanabhumiap.in 说:
2024年1月09日 14:07

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term. Students often use these educational board model papers as an integral part of their exam preparation strategy, helping them familiarize themselves with the exam pattern and refine their understanding of the subject matter.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter