Hi, I Want to simply print out 2d array elements. Method is located in
Helper module. Calling helper method from jQuery. At the moment I''m not
getting output (=> 0..3) I tried to do iterate through array via .each()
method, but it would just print en entire array, however i want to
access each element and convert it either to string or integer. can
someone point me to possible solution? thanks!
#app/helpers/controller_helper.rb
def get_array
database = SQLite3::Database.open("test.db")
result = database.execute("select * from table1")
print_array(result)
end
def print_array(array)
for i in 0..array.length
puts array[i][i]
end
end
-------------------------------------------------------------------
#app/views/controller/file.js.erb
if($(''.check_box1'').is('':checked'')){
$("#div2").html("<%= get_array%>")
}
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/ac97a96a682b25162aa93f019fb078e5%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.
On Nov 1, 2013, at 9:20 AM, leo nike <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, I Want to simply print out 2d array elements. Method is located in > Helper module. Calling helper method from jQuery. At the moment I''m not > getting output (=> 0..3) I tried to do iterate through array via .each() > method, but it would just print en entire array, however i want to > access each element and convert it either to string or integer. can > someone point me to possible solution? thanks! > > #app/helpers/controller_helper.rb > def get_array > database = SQLite3::Database.open("test.db") > result = database.execute("select * from table1") > print_array(result) > end > > def print_array(array) > for i in 0..array.length > puts array[i][i] > end > end > > ------------------------------------------------------------------- > #app/views/controller/file.js.erb > if($(''.check_box1'').is('':checked'')){ > $("#div2").html("<%= get_array%>") > } > > -- > Posted via http://www.ruby-forum.com/.You don’t want to use puts in there. You want to return the value so it gets evaluated in the erb code. puts returns nil, which means you get nothing inside the html element. I’m not exactly sure from that method what you want inside div2 either. Here’s what I’d suggest: def get_array() # sqlite stuff as you have it format_table(result) end def format_table(data) out = "<table>\n" out += data.map do |row| format_row(row) end.join("\n") out += "\n</table>\n" end def format_row(row) out = "<tr>" out += row.map do |cell| format_cell(cell) end.join out += "</tr>" end def format_cell(cell) "<td>#{cell}</td>" end This will output html code, which seems like what you’d want at that point. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/D5C254F6-6206-4D34-9BAB-E4CC1A39DE05%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Nov 3, 2013, at 3:35 PM, Tamara Temple <tamouse.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Nov 1, 2013, at 9:20 AM, leo nike <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Hi, I Want to simply print out 2d array elements. Method is located in >> Helper module. Calling helper method from jQuery. At the moment I''m not >> getting output (=> 0..3) I tried to do iterate through array via .each() >> method, but it would just print en entire array, however i want to >> access each element and convert it either to string or integer. can >> someone point me to possible solution? thanks! >> >> #app/helpers/controller_helper.rb >> def get_array >> database = SQLite3::Database.open("test.db") >> result = database.execute("select * from table1") >> print_array(result) >> end >> >> def print_array(array) >> for i in 0..array.length >> puts array[i][i] >> end >> end >> >> ------------------------------------------------------------------- >> #app/views/controller/file.js.erb >> if($(''.check_box1'').is('':checked'')){ >> $("#div2").html("<%= get_array%>") >> } >> >> -- >> Posted via http://www.ruby-forum.com/. > > You don’t want to use puts in there. You want to return the value so it gets evaluated in the erb code. puts returns nil, which means you get nothing inside the html element. > > I’m not exactly sure from that method what you want inside div2 either. > > Here’s what I’d suggest: > > def get_array() > # sqlite stuff as you have it > format_table(result) > end > > def format_table(data) > out = "<table>\n" > out += data.map do |row| > format_row(row) > end.join("\n") > out += "\n</table>\n" > end > > def format_row(row) > out = "<tr>" > out += row.map do |cell| > format_cell(cell) > end.join > out += "</tr>" > end > > def format_cell(cell) > "<td>#{cell}</td>" > end > > This will output html code, which seems like what you’d want at that point.then i decided it was too ''wet'', and refactored: def wrap(s, tag, attr={}, &b) s = [s] unless s.is_a? Array attr = attr.empty? ? '''' : " #{attr.map{|k,v| "#{k}=''#{v}''"}.join(" ")}" out = "<#{tag}#{attr}>" out += s.inject("") {|m,o| m+="#{block_given? ? (yield o) : o}"} out += "</#{tag}>" end def format_table(data) wrap(data,"table",border: 1) do |row| wrap(row,"tr") do |cell| wrap(cell,"td") end end end data=[[1,2,3,4],[5,6,7,8]] formate_table(data) # => "<table border=''1''><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>5</td><td>6</td><td>7</td><td>8</td></tr></table>" (https://gist.github.com/tamouse/7300078 ) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/BD222CFA-9460-42E5-A0DF-5B606F621707%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Great! I didnt even think about implementation like that, that''s awesome. thanks a lot! can I contact you, in case something doesnt work here? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5deb50961e0e08f76611d8236b1ddbd7%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On Nov 4, 2013, at 8:05 AM, leo nike <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Great! I didnt even think about implementation like that, that''s > awesome. thanks a lot! can I contact you, in case something doesnt work > here?Just reply on list, there are some truly awesome people here, most definitely not including me. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/B5A5A0FE-4402-45B3-AE12-FA5C574A9CB5%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Nov 4, 2013, at 3:19 AM, Tamara Temple <tamouse.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Nov 3, 2013, at 3:35 PM, Tamara Temple <tamouse.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> On Nov 1, 2013, at 9:20 AM, leo nike <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >>> Hi, I Want to simply print out 2d array elements. Method is located in >>> Helper module. Calling helper method from jQuery. At the moment I''m not >>> getting output (=> 0..3) I tried to do iterate through array via .each() >>> method, but it would just print en entire array, however i want to >>> access each element and convert it either to string or integer. can >>> someone point me to possible solution? thanks! >>> >>> #app/helpers/controller_helper.rb >>> def get_array >>> database = SQLite3::Database.open("test.db") >>> result = database.execute("select * from table1") >>> print_array(result) >>> end >>> >>> def print_array(array) >>> for i in 0..array.length >>> puts array[i][i] >>> end >>> end >>> >>> ------------------------------------------------------------------- >>> #app/views/controller/file.js.erb >>> if($(''.check_box1'').is('':checked'')){ >>> $("#div2").html("<%= get_array%>") >>> } >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >> >> You don’t want to use puts in there. You want to return the value so it gets evaluated in the erb code. puts returns nil, which means you get nothing inside the html element. >> >> I’m not exactly sure from that method what you want inside div2 either. >> >> Here’s what I’d suggest: >> >> def get_array() >> # sqlite stuff as you have it >> format_table(result) >> end >> >> def format_table(data) >> out = "<table>\n" >> out += data.map do |row| >> format_row(row) >> end.join("\n") >> out += "\n</table>\n" >> end >> >> def format_row(row) >> out = "<tr>" >> out += row.map do |cell| >> format_cell(cell) >> end.join >> out += "</tr>" >> end >> >> def format_cell(cell) >> "<td>#{cell}</td>" >> end >> >> This will output html code, which seems like what you’d want at that point. > > then i decided it was too ''wet'', and refactored: > > def wrap(s, tag, attr={}, &b) > s = [s] unless s.is_a? Array > attr = attr.empty? ? '''' : " #{attr.map{|k,v| "#{k}=''#{v}''"}.join(" ")}" > out = "<#{tag}#{attr}>" > out += s.inject("") {|m,o| m+="#{block_given? ? (yield o) : o}"} > out += "</#{tag}>" > end > > > def format_table(data) > wrap(data,"table",border: 1) do |row| > wrap(row,"tr") do |cell| > wrap(cell,"td") > end > end > end > > data=[[1,2,3,4],[5,6,7,8]] > > formate_table(data) # => "<table border=''1''><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>5</td><td>6</td><td>7</td><td>8</td></tr></table>" > > (https://gist.github.com/tamouse/7300078 ) >Listening to one of Ruby Tapas made me shake my head with pain. This: s = [s] unless s.is_a? Array should be this: s = Array.new(s) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20679126-A470-4632-BC6D-F79A315297E9%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Hi, once again thanks for you code contribution, that helped me to see
problem from a different perspective a little bit. I liked both of your
code snippets, and 1st one in particular (since i actually understand
it)
There is only one issue though, if I try to run code below on trough
jQuery (when the checkbox is checked) it wouldnt really execute
anything.
That''s what I had problem before, and I know the problem is not with
jQuery method. I cant easily modify code to
$("#div2").html("<%= ''hello''%>") and
it would print ''hello'' in that div.
So, there is something that jQuery doesnt see in iether get_array or
format_table method. I''ve tried to merge all the 4 methods into one and
run it as one method, but it still wouldnt work. Would you please check
it one again, and let me know what could be changed?
And yes, I just want to output the result of parsed array into html
code. I dont really care how, it''s just I know only Jquery .html()
method and it seems relatively simple.
Thank you very much
def get_array()
# sqlite stuff as you have it
format_table(result)
end
def format_table(data)
out = "<table>\n"
out += data.map do |row|
format_row(row)
end.join("\n")
out += "\n</table>\n"
end
def format_row(row)
out = "<tr>"
out += row.map do |cell|
format_cell(cell)
end.join
out += "</tr>"
end
def format_cell(cell)
"<td>#{cell}</td>"
end
----------------------------------------------------------------------
#app/views/controller/file.js.erb
if($(''.check_box1'').is('':checked'')){
$("#div2").html("<%= get_array%>")
}
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/672269d07190cfae6045d5d6d6e6cf6c%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.