Summarize object area with a Hash in Ruby

Posted by Arto Uusikangas on Stack Overflow See other posts from Stack Overflow or by Arto Uusikangas
Published on 2010-05-24T10:08:27Z Indexed on 2010/05/24 10:11 UTC
Read the original article Hit count: 267

Filed under:
|
|
|
|
require 'sketchup'

     entities = Sketchup.active_model.entities
     summa = Hash.new

     for face in entities
        next unless face.kind_of? Sketchup::Face
        if (face.material)
              summa[face.material.display_name] += face.area


    end
   end

Im trying to get the structure in the array as such:

summa { "Bricks" => 500, "Planks" => 4000 }

Making a ruby script for Google Sketchup btw

But if I run this code i only get

Error: #+' for nil:NilClass> C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:17 C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:14:ineach' C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:14 C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:8:in `call'

As im used to using PHP and just doing $array['myownassoc'] += bignumber; But i guess this isnt the right approach when using Ruby?

So any help in how i need to go would be nice.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about hash