Forum: Ruby mult dimensional arrays in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2008-11-17 09:53
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-17 16:59
(Received via mailing list)
2008/11/17 Sijo Kg <sijo@maxxion.com>:
> 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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2008-11-18 12:22
Hi
 Thanks for your reply
Sijo
This topic is locked and can not be replied to.