oh! Thing, in this case, is just an object.. that extends Object.. where 'red_thing' is just a string..��
so, myThing's key would just be a string..
Hi - comments below
sergio_101 wrote
> During my daily work in another framework, we are able to do something
> very
> handy like so:
>
> myThing := Thing findOrCreateByKey: 'red_thing'.
>
> where the key is some underscored all lower case unique name for an object
> we will be calling all the time.
>
> this will return either a new thing with a key of 'red_thing' or the
> current one that already exists.
In your example above what type of object is Thing?
If the object stored at red_thing is to be a global then you'd want
Thing>>findOrCreateByKey: aKey
�� ^Smalltalk at: aKey ifAbsentPut:[self createThingFor:aKey]
Look at the senders/implementors of #at:ifAbsentPut: for other ideas e.g.
Dictionary>>#at:ifAbsentPut:
> We can also do something like:
>
> myThing updateWithDictionary: aDictionary.
>
> and it will flip through the dictionary and assign values to the instance
> variables.
MyClass>>updateWithDictionary: aDictionary
�� aDictionary keysAndValuesDo:[:key :value|
�� �� �� �� self perform: key asMutator with: value].
> The above is very handy.
>
> I am currently creating a bunch of methods that populate my image with
> real
> objects, and could use these methods.
>
> My question is. Does a behavior like this already exist? If not, I'd like
> to add these methods to my project (in monticello). They will be used with
> several different objects, so should I add the methods to Object?
>
> I would imagine this is something I would want to use a great deal. How
> would I go aobut making this something I could include easily in further
> projects?
>
> I realize that the 'updateWithDictionary' is very inefficient, but this
> will just be somethign that runs once, during data import, and never
> again.
>
> I just want to make sure I am not doing anything whacky.
>
> Thanks!
--
View this message in context: http://forum.world.st/Object-select-or-create-methods-tp4799266p4799384.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.