What does this code in ruby do? -


i new here please gentle me , i'm still newbie in programming , in ruby language.

this combo box , want know goes after change or function calls. can please tell me goes or does? thanks

<p><%= f.select :done_ratio, ((0..100).step(1).to_a.collect {|r| ["#{r} %", r] }), :required => @issue.required_attribute?('done_ratio') %></p> 

it creating html select (dropdown) box, values: 0 %, 1 %, 2 %, ..., 100 %.

the field submitted part of form. may or may not required field, depending on value of @issue.required_attribute?('done_ratio'). (this presumably method in issue model, can found in ./app/models/issue.rb.)

breaking down:

  • (0..100) -- creating range object, 0 100 (inclusive).
  • .step(1) -- not needed; delete it. it's saying "step through range 1 @ time" (which default anyway). converts range enumerator.
  • .to_a -- not needed; delete it. converting enumerator array.
  • .collect {|r| ["#{r} %", r] } -- mapping array new list of arrays, like: [["0 %", 0], ["1 %", 1], ..., ["100 %", 100]]. (this method work fine on range or enumerator object, since both classes include this method enumerable module. hence why above 2 steps can both removed!)
  • f.select :done_ratio, (...) -- creating html select element called done_ratio, above names/values.

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -