Is there a way to call methods of a certain class randomly in ruby?
3 answers
One line answer:
Something.new.send(Something.instance_methods(false).shuffle.first)
Explaination
Something.instance_methods(false)
# Will give you [:A, :B, :C, :D]
Something.instance_methods(false).shuffle.first
# Will give you a random method out of it
Something.new.send(<method name>)
# Will call that random method and give you output
FROM COMMENTS (great suggestion)
You can use it like:
Something.instance_methods(false).sample
instead Something.instance_methods(false).shuffle.first
+4
source to share