A while ago, I was taking a look at a problem on the rails forum. This post was submitted to find the best solution to round any number to the nearest multiple of 10. For example, this method would take the number 6 and return 10, or the number 29 and return 30. So the first thing that popped into my mind was modulus. We can use modulus to determine how far we are from the nearest multiple of 10. Meaning that if we are given 19 and we want to know how close we are to the nearest 10, we can simple do 19 % 10, which will return 9, and 10 - 9 is 1, so we are 1 away from the nearest 10 spot. Here is that method, assuming only Fixnum, so it is implemented as an extension of the Fixnum class:
12345678
classFixnumdefroundupreturnselfifself % 10 == 0# already a factor of 10returnself + 10 - (self % 10) # go to nearest factor 10endend
While this did the job, it was suggested that it would be better if things happened.
Use the Numeric class (this class encompasses Float, Fixnum and Integer)
Don't limit the method to just the nearest 10, have it as a parameter
I first saw the need to convert a hash object to a class when answering this post.
In the post, the user wanted to load a YAML object into his hash and then present the data from the hash in a form. Needless to say it was not very DRY the way it had to be implemented. So I started looking into it, I found this. This solution was a great starting point for where I ended up, but it was not general enough, it was hard coded, plus it was missing the getters and setters. So it turns out that in ruby it wasn't too much trouble to convert a hash into a class object. So let's get started:
I have implemented this for use in Rails, so let's start with the model that does all the magic:
1234567891011
classHashitdefinitialize(hash) hash.each do |k,v|self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pairself.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variableself.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variableendendend
Notice the self.class.send(:define_method ...) rather than self.define_method, this is a hack to overcome the fact that define_method() is private. I had come across this when trying to figure out the post mentioned above. Found the information to solve this here.
Ok, so on to the Controller that creates the Hashit object:
And of course, now the view, what we wanted to clean up and make more elegant. Here is what the user started with:
Note: In this example @hashit is an actual hash, not a class object.
Much simpler, DRYer 🙂
Well that is pretty much it, I suppose the next step would be to have a save method that updates the hash? This way we can do @hashit.save() and it will return a new hash that you can use. Well actually, that probably isn't too hard, lets see if I can do it real quick, class object back to hash ...
Well, I am back and I was able to figure it out, here is the new class:
12345678910111213141516171819
classHashitdefinitialize(hash) hash.each do |k,v|self.instance_variable_set("@#{k}", v)self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})endenddefsave hash_to_return = {}self.instance_variables.each do |var| hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)endreturn hash_to_returnendend
Just added the save() method, that takes all the instance variables and sets them as keys in our new hash. So here is the outcome of our save():
Neat, this is just from the examples, more to come with opengl when I get a chance.
Check out this guy's opengl stuff, really cool!
This requires java applet support, and I can say that it works wonderfully in Linux/FireFox, and Alias (from above) says that it works with IE. So there you have it.
Update: Version 2 of Open Flash Chart is available. Examples for version 2 are here.
I just finished converting the latest version of Open Flash Chart over to ruby, for use with rails.
Check out what I can do now 🙂 Like the google analytics graphs.