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:

  1. what precisely syntax mean? expand

    questions.collect { |q| q.answers }  

    or there else going on here?

  2. 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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -