Ruby on Rails
Tuesday, June 5, 2012
On 5 June 2012 19:19, cyber c. <lists@ruby-forum.com> wrote:
Jeremy W. wrote in post #1063195:
>> for file in @files
>> @paths = A.fill_pathsYah i get it. I wanted to convert this array of hash into an array of
>> 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.
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment