简体   繁体   English

在Rails控制台中多次运行类方法

[英]running class method multiple times in rails console

This might be a stupid question, 这可能是一个愚蠢的问题,

I am running a class method inside a rails 2 console as follows: 我在rails 2控制台中运行一个类方法,如下所示:

>> load 'test_pass.rb'
=> ["TestPass"]
>> TestPass.test_data(TestPass.run_data)
=> ["test1", "test2"]

But if i wan to run the above method 50 times in loop, i tried the following: 但是,如果我想循环运行上述方法50次,则尝试以下操作:

    >> 50.times.each do |i| TestPass.test_data(TestPass.run_data)) end
    => 50

I get 50. Shouldnt it run 50 times and return me back results each time? 我得到50。它应该运行50次并每次返回结果吗?

您需要使用collect方法。

50.times.collect { TestPass.test_data(TestPass.run_data)) }

.times returns the number. .times返回数字。

If you want to collect results, use collect. 如果要收集结果,请使用收集。

50.times.collect do TestPass.test_data(TestPass.run_data)) end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM