# Applying Ruby Script to Dynamic Components

**URL:** <https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715>\
**Category:** Ruby API\
**Tags:** sketchup, ruby, components, dynamiccomponents\
**Created:** [June 22, 2022, 8:11pm UTC](https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715 "2022-06-22T20:11:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjellyman](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@Benjellyman](https://forums.sketchup.com/u/Benjellyman)\
**Post date:** [June 22, 2022, 8:11pm UTC](https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715/1 "2022-06-22T20:11:58Z")

</div>

I’ve written a ruby script for my dynamic component and I don’t know how to get it to apply changes to the model. Can anyone explain what I need to do to in order for the script to take effect

 ![ruby](https://global.discourse-cdn.com/sketchup/original/3X/a/c/ac306b657d57b08201c146977c7a4bb0858f432e.png)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [June 22, 2022, 8:55pm UTC](https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715/2 "2022-06-22T20:55:02Z")

</div>

First of all … the DC dictionaries are named `"dynamic_attributes"` not `"D_A"`.

Secondly, … changing the `"lenx"` attribute in the definition will change **all** of it’s instances. If you need to only change a single instance, (and the definition has more than 1 instance,) then you would set the attribute in the instance’s dynamic dictionary. But there are likely also other attributes that may need to exist or be changed. (There is more discussion in the DC category on this.)

> [@Benjellyman](#):
>
> Can anyone explain what I need to do to in order for the script to take effect

See this topic …

- [Method to ask a dynamic component instance to recalc itself?](https://forums.sketchup.com/t/method-to-ask-a-dynamic-component-instance-to-recalc-itself/13905)

---

<div class="post-metadata">

**Author:** ![Benjellyman](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@Benjellyman](https://forums.sketchup.com/u/Benjellyman)\
**Post date:** [June 23, 2022, 2:05pm UTC](https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715/3 "2022-06-23T14:05:09Z")

</div>

Thank you so much for your response.  
And please forgive my ignorance on this matter, I tried to implement what I found in that forum.  
If you can, please continue to point me in the right direction, as I am trying to make the right corrections but I’m unsure if I’m getting any closer to getting this working.

this is what I currently have

```ruby
entities = Sketchup.active_model.entities
definition = Sketchup.active_model.definitions[0]
transformation = Geom::Transformation.new([0,0,0])
componentinstance = entities.add_instance(definition, transformation)
definition = componentinstance.definition
status = definition.set_attribute "dynamic_attributes", "_lenx_nominal", "125"

def recalc_dc(inst)
  return nil unless defined?($dc_observers)
  if inst.is_a?(Sketchup::ComponentInstance) &&
  inst.attribute_dictionary('dynamic_attributes')
    $dc_observers.get_latest_class.redraw_with_undo(inst)
  end
  nil # (there is no particular return value)
end
```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [June 23, 2022, 6:29pm UTC](https://forums.sketchup.com/t/applying-ruby-script-to-dynamic-components/201715/4 "2022-06-23T18:29:59Z")

</div>

In order for the `recalc_dc` method to run, your code needs to call it.  
The preamble code should also be within a method.  
And both methods should be in a submodule of your unique namespace module.

Time to learn basic Ruby programming. See …

> [@Ruby Learning Resources \[WikiLists\]](https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861/):
>
> All seven of the lists below are set as wikis. card_index INDEX [notebook PRIMERS & GUIDES](http://forums.sketchup.com/t/ruby-learning-resources/22861/2)[books BOOKS](http://forums.sketchup.com/t/ruby-learning-resources/22861/3)[mortar_board TUTORIALS](http://forums.sketchup.com/t/ruby-learning-resources/22861/4)[card_box REFERENCE](http://forums.sketchup.com/t/ruby-learning-resources/22861/5)[clipboard TEMPLATES](http://forums.sketchup.com/t/ruby-learning-resources/22861/6)[newspaper NEWS](http://forums.sketchup.com/t/ruby-learning-resources/22861/7)[bulb IDEAS](http://forums.sketchup.com/t/ruby-learning-resources/22861/8)
