[LRUG] [Help request] Programmatically inserting user data into js chart
Oto Brglez
otobrglez at gmail.com
Wed Jun 8 23:20:08 PDT 2016
Hey!
Loose "JSON.pretty_generate" at the and new-lines will go away.
Cheers!
- Oto
On Wed, Jun 8, 2016 at 11:53 PM Jesse Waites <jesse.waites at gmail.com> wrote:
> Update: I tried it with Tom's pretty generate JSON method and am a little
> closer - @jsonish is now:
>
> @jsonish: "[\n {\n \"key\": 0,\n \"value\": 5\n },\n {\n \"key\": 1,\n
> \"value\": 3\n },\n {\n \"key\": 2,\n \"value\": 2\n }\n]"
>
> So looks like I need to strip the new lines out? Or rather, not put them
> in in the first place? Its good to know I was close in my solution path.
>
> On Wed, Jun 8, 2016 at 1:03 PM, John Cinnamond <jc at panagile.com> wrote:
>
>> 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:
>>
>> array.each_with_index.map { |value, idx| … }
>>
>> You can say:
>>
>> array.map.with_index { |value, idx| … }
>>
>> This only saves you 5 characters, but I think it makes the intent
>> slightly clearer.
>>
>>
>> On Wed, 08 Jun 2016 at 17:57 Tom Stuart <Tom Stuart
>> <Tom+Stuart+%3Ctom at codon.com%3E>> wrote:
>>
>>> On 8 Jun 2016, at 14:32, Jesse Waites <jesse.waites at gmail.com> wrote:
>>> > def make_jsonish(array)
>>> > array.each_with_index do |num, i|
>>> > puts "{ key: #{i}, value: #{num}},"
>>> > end
>>> > end
>>>
>>> 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:
>>>
>>> def make_jsonish(array)
>>> array.each_with_index.map { |num, i|
>>> "{ key: #{i}, value: #{num}},"
>>> }.join("\n")
>>> end
>>>
>>> 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:
>>>
>>> def make_jsonish(array)
>>> elements = array.each_with_index.map { |num, i|
>>> { key: i, value: num}
>>> }
>>> JSON.pretty_generate(elements)
>>> end
>>>
>>> Cheers,
>>> -Tom
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
>>> Manage your subscription:
>>> http://lists.lrug.org/options.cgi/chat-lrug.org
>>> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>
>>
>>
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
>> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
>> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>
>>
>
>
> --
> Jesse Waites
> JesseWaites.com
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160609/4a49147b/attachment-0002.html>
More information about the Chat
mailing list