Ruby: methods as array elements - how do they work?

前端 未结 6 1821
一向
一向 2021-02-19 05:58

This probably isn\'t something you should try at home, but for some reason or another I tried to create an array of methods in Ruby.

I started by defining two methods.

6条回答
  •  梦谈多话
    2021-02-19 06:58

    If you had a square method and wanted to create an array with the square values of 2 and 4, you would write

    array = [square(2), square(4)]
    

    Here you are doing exactly the same thing, except that your test methods don't return anything and that's why your final array seems empty (actually, it contains [nil, nil]).

提交回复
热议问题