Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return a SpaceType, and then get it's name and convert to a string. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...).

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return a SpaceType, and then get it's name and convert to a string. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...).

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return a SpaceType, and then get it's name and call .name which returns an optional string so convert to a string. that to a string using .to_s. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...).

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return a SpaceType, and then call .name which returns an optional string so convert that to a string using .to_s. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...).two...). I like to store such tables in an array of hashes, as it is very convenient to work with in Ruby if you need to some more processing, and really easy to export to a CSV file using the standard bit of code I'm posting here.

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get space.spaceType.get that will return a SpaceType, an actual SpaceType, and then call .name which returns an optional string so convert that to a string using .to_s. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...). I like to store such tables in an array of hashes, as it is very convenient to work with in Ruby if you need to some more processing, and really easy to export to a CSV file using the standard bit of code I'm posting here.

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return an actual SpaceType, and then call .name which returns an optional string so convert that to a string using .to_s. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...). I like to store such tables in an array of hashes, as it is very convenient to work with in Ruby if you need to some more processing, and really easy to export to a CSV file using the standard bit of code I'm posting here.

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = space.spaceType.get.name.to_s
  else
    space_type = "No Space Type Defined"
  else
     space_type = space.spaceType.get.name.to_s
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end