Access `self` of an object through the parameters

前端 未结 4 747
太阳男子
太阳男子 2021-01-19 13:40

Let\'s say I want to access an element of an array at a random index this way:

[1, 2, 3, 4].at(rand(4))

Is there a way to pass the size of

4条回答
  •  执笔经年
    2021-01-19 14:38

    You could do something similar with lambda:

    getrand = ->(x) { x[rand(x.count)] }
    getrand.call [1,2,3]
    # => 2 
    

提交回复
热议问题