ruby on rails block sytax using &: -
so have question model , answer model , question has_many answers (it's multiple choice question).
suppose questions collection of question objects.
in order collect answers can this:
questions.collect(&:answers)
two questions:
what precisely syntax mean? expand
questions.collect { |q| q.answers }
or there else going on here?
is there way
questions.collect { |q| q.answers.shuffle }
using same syntax?
collect(&:answers.shuffle)
isn't doing it.
i can't seem find in tutorials on ruby blocks on web , searching doesn't work (search engines ignore "&:"). found in inherited code.
thanks
yes, first question n-duplicated, regarding second: no, cannot chain methods. however, nothing stops -other writing code may puzzle people- create own tool:
class symbol def to_proc proc |obj| self.to_s.split(/\./).inject(obj, :send) end end end p ["1", "2", "3"].map(&:"to_i.succ") # [2, 3, 4]
you can find ways send arguments, though won't beautiful.
Comments
Post a Comment