Ruby's Math module includes functions for a natural log (log to the base e) and the common log (log to the base 10). If you have to have a log to an arbitrary base, you have to write your own. Doing so is trivial.
I will probably explain the theory behind this another day.
Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts
Monday, January 25, 2010
Wednesday, December 23, 2009
Markov Chains
You know developers have an itch to implement something cool that they learned about. No can say it better than XKCD.

I learnt about Markov Chains today.
It was so cool that I decided to do something with it. So I wrote a simple helper that generates articles for the app that I was testing. Here is the code in all its glory. It is not the fastest piece of code and is a memory hog, but who cares?
This is how you use it.
And this produced this fine article. Isn't this cool?
I learnt about Markov Chains today.
In mathematics, a Markov chain, named after Andrey Markov, is a discrete random process with the Markov property. A discrete random process means a system which can be in various states, and which changes randomly in discrete steps. It can be helpful to think of the system as evolving once a minute, although strictly speaking the "step" may have nothing to do with time. The Markov property states that the probability distribution for the system at the next step (and in fact at all future steps) only depends on the current state of the system, and not additionally on the state of the system at previous steps. Since the system changes randomly, it is generally impossible to predict the exact state of the system in the future. However, the statistical properties of the system at a great many steps in the future can often be described. In many applications it is these statistical properties that are important. - From the wikipedia entry on Markov Chains
It was so cool that I decided to do something with it. So I wrote a simple helper that generates articles for the app that I was testing. Here is the code in all its glory. It is not the fastest piece of code and is a memory hog, but who cares?
This is how you use it.
And this produced this fine article. Isn't this cool?
Sunday, January 14, 2007
The Observer Pattern using Ruby Meta- Voodoo
The ruby standard library already has an implementation for the observer pattern. The implementation of the pattern was not very rubyish. It seemed a direct implementation of the Java equivalent of it. Ruby is far more powerful. This is the more rubyish version of the of the code goes something like this.
You can find the code for observerext.rb here. What I have basically done is to extend the Module class, which defines the observable method. The observable method injects the attributes' getters and setters. The setter method notifies the observers if there are any. The Observer module does the leg work of registering and unregistering the blocks and the notifyobservers method.
Update: Forgot to credit Greg Houston for explaining some of the ruby voodoo.
require 'observerext'
class Person
include Observer #include the observer related methods
observable :name,:address # the metavoodoo to create the accessor methods and fire events
end
admin=Person.new
admin.name='vagmi'
admin.address='admin@somesite.com'
# first observer
admin.observe :name do |old,newvalue|
puts %{The administrator changed from #{old} to #{newvalue}}
end
# the second observer
admin.observe :name do |old,newvalue|
puts 'there can be multiple observers'
end
admin.name='root'
#this prints the following statements
# > The administrator has changed from vagmi to root
# > there can be multiple observers
You can find the code for observerext.rb here. What I have basically done is to extend the Module class, which defines the observable method. The observable method injects the attributes' getters and setters. The setter method notifies the observers if there are any. The Observer module does the leg work of registering and unregistering the blocks and the notifyobservers method.
Update: Forgot to credit Greg Houston for explaining some of the ruby voodoo.
Subscribe to:
Posts (Atom)