Greetings. I am trying to list all keys in a java.util.HashMap field. The Java code to do this looks like: # Collection c = hMap.values(); # # //obtain an Iterator for Collection # Iterator itr = c.iterator(); # # //iterate through HashMap values iterator # while(itr.hasNext()) System.out.println(itr.next()); So, I took a stab at the ruby version: # I have a java hashmap called fields that was created using iText thusly reader = pdfreader.new( "pdf_file.pdf" ) @form = reader.getAcroFields() fields = @form.getFields() # this is a hashmap of all fields on form fields._classname # entere in irc displays: java.util.HashMap # So, following the java code above c = fields.values() itr = c.iterator() while itr.hasNext() p itr.next() end Instead of getting the field names, I get #<#<Class:0x2d47d74>:0x2c15208> #<#<Class:0x2d47d74>:0x2c151a4> #<#<Class:0x2d47d74>:0x2c150b4> (there are three keys in the hash) Any ideas? Thanks, Dan -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-May-25 21:48 UTC
Re: Looping through java hashmap from ruby through rjb
On May 25, 9:28 pm, Dan Sadaka <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Greetings. > > I am trying to list all keys in a java.util.HashMap field. > > Instead of getting the field names, I get > #<#<Class:0x2d47d74>:0x2c15208> > #<#<Class:0x2d47d74>:0x2c151a4> > #<#<Class:0x2d47d74>:0x2c150b4> > > (there are three keys in the hash) >Well p (or puts etc) end up calling to_s on objects, and you''re obviously getting Object''s default to_s. Does rjb have some method to convert java strings into ruby strings ? Fred> Any ideas? > > Thanks, > Dan > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On May 25, 9:28�pm, Dan Sadaka <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> > Well p (or puts etc) end up calling to_s on objects, and you''re > obviously getting Object''s default to_s. Does rjb have some method to > convert java strings into ruby strings ? > > FredFred, Good question. I did some research and found enough to try using itr.next().to_string and guess what. It worked! I had tried ruby''s .to_s and naively thought that rjb was translating it to the java equivalent because I wasn''t getting an error. Thanks-a-bunch for the guidance, Dan -- Posted via http://www.ruby-forum.com/.