<img style="border: none; background:none; width: 0; height: 0;" src="https://welovepg.polymail.io/v2/z/a/NTc1ODRlZjcyMjhh/us2rHmsD3TsNgdd9TPWvQekkJkmoPlftRS-U7MLA3nXNcdOBglpQM6GzoEw_FyBdNk0dKV2FcEgCi12gF0VdfJMoh36lNC89fBXnzPe1lbDkilafd0HK_j2UpDCRdwZA3Jtn7U3PfeY9Onzlvmk=.png" alt="" width="0px" height="0px" border="0" />As an aside, `map` returns an enumerator if you don’t give it a block, and enumerators have a `with_index` method. So instead of:<div><br></div><div>    array.each_with_index.map { |value, idx| … }</div><div><br></div><div>You can say:</div><div><br></div><div>    array.map.with_index { |value, idx| … }<br><br><div id="psignature"><div>This only saves you 5 characters, but I think it makes the intent slightly clearer.</div><div><br></div><div style="font-size:10px; color: #7E8F9F;"></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div dir="ltr">On Wed, 08 Jun 2016 at 17:57 Tom Stuart <tom@codon.com> <<a href="mailto:Tom Stuart <tom@codon.com>">Tom Stuart <tom@codon.com></a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>On 8 Jun 2016, at 14:32, Jesse Waites <<a href="mailto:jesse.waites@gmail.com">jesse.waites@gmail.com</a>> wrote:<br>> def make_jsonish(array)<br>>   array.each_with_index do |num, i|<br>>     puts "{ key: #{i}, value: #{num}},"<br>>   end<br>> end<br><br>This looks pretty close to being right, but you want to return a string by joining all the individual lines, not print them out. For example:<br><br>def make_jsonish(array)<br>  array.each_with_index.map { |num, i|<br>    "{ key: #{i}, value: #{num}},"<br>  }.join("\n")<br>end<br><br>Less significantly, it would be better to rely on Ruby to turn the resulting data structure (including enclosing square brackets) into JSON for you, so that you’re not responsible for the conversion yourself:<br><br>def make_jsonish(array)<br>  elements = array.each_with_index.map { |num, i|<br>    { key: i, value: num}<br>  }<br>  JSON.pretty_generate(elements)<br>end<br><br>Cheers,<br>-Tom<br>_______________________________________________<br>Chat mailing list<br><a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br></p><div></blockquote></div><br></div>