Show newer

For any ruby :ruby: devs out there, wanted to share a neat little open-source :opensource: module I wrote to solve a common problem. Keep in mind ruby is not my main language so if there is a batter approach here I'm all ears.

Basically right now I have a game (text based mud) and in normal and expected fashion objects in the game are created when their objects get initialized, such as a new player being created. The system then periodically saves the universe by marshalling all the object into some serialized format and saves it to a file from time to time. As tends to be the case with serialization, however, when an object is restored, such as a previous player logging out and back in, then the class is created directly without the initialize method getting called , its class and instance variables are simply populated directly.

This is where problems can arise if you change the system and add new features (such as a new variable to an object). New objects that are created will populate the new variable correctly through the initialize method however already existing players will not have that variable set at all (it won't be nil, it simply wont be set, which is a distinctly different state). This is the problem I solved.

What I did was created a mix-in module that lets you set default values for variables, once a class is reinstated from storage it checked if any variables that have defaults are unset (nil variables are considered set) and then applies the default value to them. In this way legacy objects will be able to update to new code changes automatically when it loads. To prevent duplicating code you can even intentionally leave it out of the initialize method and rely on the defaults when it is appropriate to do so.

Moreover the defaults do not have to be static values but can be determined based on the existing state of the object, which makes them dynamic and flexible... a class that uses the mixin could look like this:

require 'defaults'

class Foo
include Defaults

# @bar defaults to @baz*2
default(:bar) { |this| this.baz * 2 }
# @faaboo defaults to 178
default(:faaboo) { 178 }

def initialize
@baz = 13

line can be ommited
@bar = 26
# if you add this line instead
load_defaults
work fine
end
end

Here is a link to the module:

git.qoto.org/aethyr/Aethyr/-/b

You can see a class that utilizes this new feature here:

git.qoto.org/aethyr/Aethyr/-/b

@Science

WOA WOa Woa.... they made another stargate movie in 2018!!!!! How in the flying fuck did I miss this! Clearly I am not getting all my memos.

Stargate Origins: Catherine

Watching the hell out of this now... I cant believe there has been a stargate movie I missed and no one told me...

@lucifargundam They so didnt give a fuck in this movie that in one scene when they drive down an LA road Dar (the lead character) sees a movie theater that says "Beastmaster II: through the portal of time" and he looks shook and points at it questioningly... lol 0 fucks given

"The natives call it 'L. A.'"

All you need to know to determine the quality of the movie I'm watching right now.

@freemo

I once had a coworker who called C# (the programming language) C Octothorpe,

pronounced cocktothorpe.

I miss the old days of hacking where you could hangup all the phones in a room using a whistle, and could have your schools computer with a QBasic program... ahhh the good old days.

From now on I am calling Hashtags Octothorpe-tags, that is all.

phys.org/news/2021-02-nuclear-
Finding out squeezed proton which move freely in nucleus, invisible to the strong force.....

LOL Someone wrote a program to mine bitcoin off a Xerox Alto (First personal computer)... Where the hell do people find the time for this shit!

Life hack.. instead of flossing just eat lots of cheap meat, its natures floss!

Liberal: "We need to raise the minimum wage to fix our problems".

Me: "It doesnt fix problems it makes them worse, here is some hard data proving it actually does more harm to the poor than good!"

Conservatives: "Yes, we agree, stupid libtards, lets get rid of minimum wage!"

Me: "Yes, great, perfect, glad you understand. So once we get rid of it how do you plan to replace it with something better that fixes the poverty problem we are facing?"

Conservatives: "Fix the what now? No no, we just oppose welfare, we dont want to fix poverty, they are just lazy"

Me: "Fuck off!"

@trinsec - peaceful neutral and good in hydrological engineering like all Dutch

@borrof - LEWD NSFW and NOT FOR THE EASILY OFFENDED (literally pig)

@freemo - Murican and good data analysis bot, also likes Dutch stonks.

@seek - Tropical islander frustrated with bedbugs and mosquitos

Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.