Hi , I have created an array of hashes using Model file class Class A < ActiveRecord::Base def fill_paths paths = Array.new for file in @files path = {} path["a"] = file path["b"] = DATA2(assume data2 is valid) paths << path end return paths end in controller class @paths = A.fill_paths for path in @paths puts path.a end I cant access path.a, which says "undefined method ''a'' for{"a"=>value1,"b"=>value2}:Hash //value1, value2 are valid data Please let me know whats wrong with this. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 Jun 2012, at 18:58, "cyber c." <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi , > > I have created an array of hashes using > > Model file class > > Class A < ActiveRecord::Base > > def fill_paths > paths = Array.new > for file in @files > path = {} > path["a"] = file > path["b"] = DATA2(assume data2 is valid) > paths << path > end > return paths > end > > in controller class > > @paths = A.fill_paths > for path in @paths > puts path.a > end > > I cant access path.a, which says > "undefined method ''a'' for{"a"=>value1,"b"=>value2}:Hash > //value1, value2 are valid data >path[''a''] is the correct syntax for accessing hashes.> Please let me know whats wrong with this. > > -- > 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeremy W. wrote in post #1063195:> On 5 Jun 2012, at 18:58, "cyber c." <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> for file in @files >> @paths = A.fill_paths >> for path in @paths >> puts path.a >> end >> >> I cant access path.a, which says >> "undefined method ''a'' for{"a"=>value1,"b"=>value2}:Hash >> //value1, value2 are valid data >> > > path[''a''] is the correct syntax for accessing hashes.Yah i get it. I wanted to convert this array of hash into an array of class objects. How do i do that in ruby? -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 June 2012 19:19, cyber c. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Yah i get it. I wanted to convert this array of hash into an array of > class objects. How do i do that in ruby?They are "class objects" - hashes are instances of Hash class ... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 June 2012 19:19, cyber c. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Jeremy W. wrote in post #1063195: > > On 5 Jun 2012, at 18:58, "cyber c." <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > >> for file in @files > >> @paths = A.fill_paths > >> for path in @paths > >> puts path.a > >> end > >> > >> I cant access path.a, which says > >> "undefined method ''a'' for{"a"=>value1,"b"=>value2}:Hash > >> //value1, value2 are valid data > >> > > > > path[''a''] is the correct syntax for accessing hashes. > > Yah i get it. I wanted to convert this array of hash into an array of > class objects. How do i do that in ruby? >Yes, you can. This is your program, refactored to do that (I''ve not tested it so excuse any typos): class Data attr_reader :a, :b def initialize(a, b) @a = a @b = b end end class A < ActiveRecord::Base def fill_paths @files.map |file| Data.new(file, data_2(''assume data2 is valid'')) end end end #Controller @paths = A.fill_paths @paths.each do |path| puts path.a end> > -- > 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote in post #1063197:> On 5 June 2012 19:19, cyber c. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Yah i get it. I wanted to convert this array of hash into an array of >> class objects. How do i do that in ruby? > > They are "class objects" - hashes are instances of Hash class ...In the index.html.erb file the following code doesnt work, paths contain valid data populated in the controller class. <% @paths.each do |path| %> <tr> <td><%= path.a %></td> -- Since paths is an array of hash path[''a''] should work It says undefined method ''a'' for {"a"=>".", "b"=>"."}:Hash Changing path.a to path[''a''] gives me an error "No route matches {:a=>".", :b=>".", :action=>"edit", :controller=>"file_paths"}" Note: . is a valid data (referring to a path in a dir) -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Jeremy, Thanks for the reply. I want to populate my DB during init one time by crawling through a set of directories with filenames and paths. How do i do this? -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 June 2012 23:01, cyber c. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Jeremy, > > Thanks for the reply. > I want to populate my DB during init one time by crawling through a set > of directories with filenames and paths. How do i do this? >Where, or how? Where: db/seeds.rb How: Iterate through directories (read the Dir/File/Path classes'' documentation) and create a record in the database for each. If either of those steps seem too complex then you need to read some tutorials on Ruby (for the first bit) and Rails (for the later).> > -- > 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@jeremy Thanks alot. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.