Skip to content

Instantly share code, notes, and snippets.

@tcotav
Created December 13, 2013 18:18
Show Gist options
  • Save tcotav/7948702 to your computer and use it in GitHub Desktop.
Save tcotav/7948702 to your computer and use it in GitHub Desktop.
dynamically adding a variable to an object (for tagging source, for example)

adding a variable into the object

Here's now we dynamically add that variable/accessor on an already created object:

def tagList(item_list)
item_list.each do |item|
  item.class.module_eval { attr_accessor :source_type}
  item.source_type = some_source_type_A
done
itemlist

in our test.rb, we dump that just to show that it made it:

item_list=tagList(original_list)

item_list.each do |item|
puts "source type: #{item.source_type}")
done 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment