Mult dimensional arrays in ruby


#1

Hi
What I am trying to do below is I have to enter two rows to the table
contact_address_types like

description(string) default_item(boolean) enabled(boolean)
Home f t
Business f t

Then I tried like

CONTACT_ADDRESS_TYPES = [[“Home”,“f”,“t”],[“Business”,“f”,“t”]]
def self.up
CONTACT_ADDRESS_TYPES.each do |array|
ContactAddressType.create(:description => array[0])
ContactAddressType.create(:default_item => array[1])
ContactAddressType.create(:enabled => array[2])
end

end

  But this not working Is this the correct way to do this?Please

help

Thanks in advance
Sijo


#2

2008/11/17 Sijo Kg removed_email_address@domain.invalid:

CONTACT_ADDRESS_TYPES = [[“Home”,“f”,“t”],[“Business”,“f”,“t”]]
def self.up

What is self here?

CONTACT_ADDRESS_TYPES.each do |array|
ContactAddressType.create(:description => array[0])
ContactAddressType.create(:default_item => array[1])
ContactAddressType.create(:enabled => array[2])
end

You need to use #map! instead of #each if you want to change
CONTACT_ADDRESS_TYPES in place. However, I’d think this is bad design
because you are changing the types of objects in a constant Array. I’d
rather do it differently (see below):

end

 But this not working Is this the correct way to do this?Please

How about

irb(main):001:0> ContactAddressType = Struct.new :decription,
:default_item, :enabled
=> ContactAddressType
irb(main):002:0> CONTACT_ADDRESS_TYPES = [
irb(main):003:1* ContactAddressType.new(“Home”, false, true),
irb(main):004:1* ContactAddressType.new(“Business”, false, true),
irb(main):005:1* ]
=> [#<struct ContactAddressType decription=“Home”, default_item=false,
enabled=true>, #<struct ContactAddressType decription=“Business”,
default_item=false, enabled=true>]
irb(main):006:0>

?

Cheers

robert


#3

Hi
Thanks for your reply
Sijo