Esoteric Ruby Blog http://cilibrar.com/~cilibrar/erblog.cgi RubLog en-us /mnt/cjhttpd/home/cilibrar/stuff/esotericruby/RubyKata/katasix.rb.html http://cilibrar.com/~cilibrar/erblog.cgi/RubyKata/KataSix.rb.html <html> <head> <title>/mnt/cjhttpd/home/cilibrar/stuff/esotericruby/RubyKata/katasix.rb.html</title> <meta name="Generator" content="Vim/6.3"> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#000000" text="#ffffff"> <pre> <font color="#00ffff"><h2><b>Ruby</b></font> <font color="#00ffff"><b>Kata</b></font> <font color="#00ffff"><b>Six</b></font> -- <font color="#00ffff"><b>Anagram</b></font></h2> <font color="#00ffff"><b># This was a fun one; another classic algorithm popular in class but always</b></font> <font color="#00ffff"><b># fun to recreate. This reminds me also of the related Boggle type problem.</b></font> <font color="#00ffff"><b># Here's my answer for the anagram list. Just feed the list of words to stdin.</b></font> <font color="#00ffff"><b>#!/usr/bin/env ruby</b></font> <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>toHistogram</b></font>(str) res = [ ] str.each_byte { <font color="#00ffff"><b>|i|</b></font> res &lt;&lt; i } res.sort <font color="#8080ff"><b>end</b></font> result = <font color="#00ffff"><b>STDIN</b></font>.readlines.inject({}) <font color="#ffff00"><b>do</b></font> <font color="#00ffff"><b>|n, w|</b></font> w.chomp! h = toHistogram(w) n[h] ||= [ ] n[h].push(w) n <font color="#ffff00"><b>end</b></font> goodkeys = result.keys.sort_by { <font color="#00ffff"><b>|i|</b></font> -result[i].size } goodkeys.each { <font color="#00ffff"><b>|k|</b></font> v = result[k] puts v.join(<font color="#ff6060"><b>'</b></font><font color="#ff40ff"><b> </b></font><font color="#ff6060"><b>'</b></font>) <font color="#ffff00"><b>if</b></font> v.size &gt; <font color="#ff40ff"><b>1</b></font> } </pre> </body> </html> <a href="http://cilibrar.com/~cilibrar/erblog.cgi/RubyKata/KataSix.rb.html">[more]</a> Ruby Kata One -- Supermarket Pricing http://cilibrar.com/~cilibrar/erblog.cgi/RubyKata/KataOne.txl <p>I&#8217;ve started Dave Thomas&#8217; series of programming Kata.</p> <p>1. does fractional money exist? nope. Just integers expressing the smallest monetary unit, e.g. pennies. There may also be a currency indicator in the case of international interest.</p> <p>2. when (if ever) does rounding take place? rounding happens according to the rules of integer truncation when RationalPrice items are bought. That is, the price is rounded towards 0. For other cases there is no rounding as it is just addition or subtraction of integers.</p> <p>3. how do you keep an audit trail of pricing decisions (and do you need to)? Yes, you need to. And an audit of money changes. These should both be implemented at a low logical level. In rails I would recomend the model have some support here. I would use Log file messages to store all pricing adjustments including username of adjuster, time, and new price. Same for all monetary balance adjustments.</p> <p>4. are costs and prices the same class of thing? In my mental model, they are. However prices are set by fiat by the vender whereas costs are set by those vendors I buy from. Therefore, if we are all using the same system, costs and prices are the same type of thing but under different authorities. In my model, Price is a base class. From Price we derive several subclasses: RationalPrice (x Units of Product y for Z money), DiscreteTablePrice (lookup table of prices and units, for implementing buy x get y free type stuff), SimplePrice ( facade over RationalPrice to allow for default of 1 unit), and so on. All of these provide a method buy that takes an integer quantity and a BuyableUnit and return a price for the transaction along with any extra decorator benefits like free warranty or whatever or nil if it is not possible to buy this item in that quantity or unit.</p> <p>Then elsewhere there must be some sort of uniform buy strategy that takes a large quantity, like 100 cans, and accesses a DiscreteTablePrice holding the buy 2 get 1 free offer. It sees it can buy 1 can for 1 dollar, 2 cans for 2 dollars, or 3 cans for 2 dollars, determines that the best unit price is had with the promotional option, and takes it over and over up to 99 cans then just simply buys one at the end. This can all be done as a simple integer partitioning problem regardless of the &#8220;real&#8221; underlying units.</p> <p>The pound example shows that we need a BuyableUnit class that will support the weights and measures necessary. This is probably worth not rewriting but just copying.</p> <p>5. if a shelf of 100 cans is priced using &#8220;buy two, get one free&#8221;, how do you value the stock? As 33 pairs plus one can = 33 * 3 + 1 cans = 100 cans for the price of 67 cans. This value can be arrived at by using a simple greedy strategy.</p> Gtk and Rails: a strange meeting http://cilibrar.com/~cilibrar/erblog.cgi/Projects/GtkRails.html Well, I woke up this morning with a strange idea in my head. I was thinking about how cool Rails is, and how sad it is that so few people get to know it right now. I then remembered another nice library called Gtk, the Gnu version of a portable GUI toolkit, and I remembered the Ruby binding for it. Then I thought, what if you wrote something in rails that provided a Gtk-like interface. So that you could run ruby Gtk programs under a rails web application. It would have to dynamically generate the html that would best approximate the Gtk calls that have been issued. An hour and a half later, rtk was born: <img src="http://cilibrar.com/~cilibrar/tmp/gtkrails.jpg"> <a href="http://cilibrar.com/~cilibrar/tmp/rtk.tar.gz">Download rtk.tar.gz source</a> Here is the <a href="http://ruby-gnome.sourceforge.net/tutorial/x276.html">Two Buttons Ruby-Gnome example</a> that this is based on. And here is the controller file where the GtkRails code is embedded: <pre> class MulController < ApplicationController private def zputs(*args) args.each { |i| Gtk::Window.getMessageConsole.addmsg(i) } end def greeting(num) zputs "This is button #{num}" end public def ui # @w = @session['w'] evt = @params['event'] id = @params['id'] Gtk::Signallable.send_signal(id, evt) if evt && id rt() render :action=>'rt' end def rt unless @w = Gtk::Window.getTopWindow # Build the GUI window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) hbox = Gtk::HBox.new(false,0) button1 = Gtk::Button.new("button 1") button2 = Gtk::Button.new("button 2") but3 = Gtk::Button.new("but3") hbox.pack_start button1, true, true, 0 hbox.pack_start button2, true, true, 0 window.add hbox window.add but3 # Callbacks and configuration. window.set_title "Two buttons" window.border_width 10 window.signal_connect('delete_event') { false } window.signal_connect('destroy') { Gtk.main_quit } button1.signal_connect('clicked') { greeting(1) } button2.signal_connect('clicked') { greeting(2) } but3.signal_connect('clicked') { zputs "You pressed button 3!" } # All done. window.show_all Gtk.main @w = Gtk::Window.getTopWindow end end end </pre> <a href="http://cilibrar.com/~cilibrar/erblog.cgi/Projects/GtkRails.html">[more]</a> Katrina Hurricane Relief with Rails http://cilibrar.com/~cilibrar/erblog.cgi/Projects/KatrinaReliefWithRails.html <p>I have been involved in a new Wiki to work together to assist the relief effort for Hurricane Katrina. We have been coordinating among all the various organizations, people, and communication networks to try to help people. We have gotten a lot of press from CNN, AP, BusinessWeek, NPR, and others. And we are getting about 300,000 hits a day at last check. Have a look at our website at <a href="http://katrinahelp.info/">http://katrinahelp.info/</a> (or <a href="http://katrina.internet2.edu/"> http://katrina.internet2.edu/</a> if that name above doesn't work) We have a massive dual processor system for this project to take heavy loads. We have gotten an army of volunteer typists transcribing missing person records into a database. About a dozen people have written scripts to grab data off other sites and convert them to this XML/RSS format called PFIF, the PeopleFinder Interchange Format. The major importance of this format is that it is made for sharing info from the ground up instead of hording all missing person info in one central database. I have been doing a little Ruby scripting myself to convert to this. The problem now is one of final integration and front end work; I am not that good at XML yet but given we have more than 100,000 records already collected it would be a shame if it is only PHP and Java that works out. Are any of the Ruby masters available for saving the world? If so please check out our status on the website and see if you might enjoy saving people's live and helping them be happy during a very hard time. We can also promote Ruby a lot with this press and show how good Ruby is for this kind of thing just for Ruby. I would also like to have just a 1-screen dirt simple Rails app going and wonder if there is any wider interest in the community. I hope to see some of you on the website soon. <a href="http://cilibrar.com/~cilibrar/erblog.cgi/Projects/KatrinaReliefWithRails.html">[more]</a> WIPO, me, and you http://cilibrar.com/~cilibrar/erblog.cgi/Projects/YouMeAndWipo.html <p>I'm not sure how many have noticed, but this year is very exciting not just for Ruby. And not just for Rails. Things are taking off in all sorts of directions now that blogging seems to have tipped off a sort of digital renaissance of writing, interaction, and understanding across all types of boundaries. Now I am going to try something a bit new, and suggest that some of the cohesion we have developed over the years in the Ruby community may be put to good use. It involves WIPO, the international treaty on intellectual property rights. And science, medecine, the nature of politics and capitalism, and many more strange and surprising things are in the mix. <p>It happens that politics are not always aligned with science, and sometimes the discrepancy can be severe. This fact is nowhere more obvious to me than my own personal experience trying to cure a potentially fatal degenerative liver condition that I have suffered my entire life. It is called hepatitis C, and it kills many more people in USA and Japan than AIDS does. For several reasons, research regarding this disease has been drastically underfunded, to the tune of about 100 times less money per death than HIV. Unfortunately, due to the nature of hepatitis C (HCV), it often goes undiagnosed for decades before causing serious liver problems, loss of ability to work, and death. Another problem is the lack of political capital in the affected groups, primarily jail prisoners and injection drug users in the United States. As these groups have essentially zero political power at this moment in time, they have been given near zero funding over the course of the last 15 years as this disease has come out into the public eye. In practical terms, this matters to me because I received HCV from a tainted blood transfusion at birth and my liver has been slowly degrading all my life. I understand that in Canada and the UK, there is government compensation for the HCV-tainted blood victims. I understand in USA there is over 100 thousand dollars per case compensation for HIV tainted blood victims. <br> <a href="http://village.vossnet.co.uk/c/crina/pag-statistics.html">here are some funding statistics</a> <br> How come USA has not yet even acknowledged officially that there was a problem with infecting thousands of people like me with HCV from a tainted blood supply? I can tell that in not many more years I will be unable to work without medical advances. Which is why I am asking any of you now who might be touched by this story to look a little further and consider putting your name on the following petition for more funding from anywhere: <br> <a href="http://www.thepetitionsite.com/signatures/703119152?page=1">http://www.thepetitionsite.com/signatures/703119152?page=1</a> <p>Don't get too hung up on the bad spelling etc on the petition. I didn't make it. I think we need many concurrent efforts to really drive this home. The reason I post this to the Ruby blog is because I understand that Japan also shares with USA a unique problem with HCV, and indeed Japan has been a leading HCV research center worldwide. Some communities in Japan have HCV rates in excess of 20%. I expect that there are more than a few people across the ocean in the Ruby community that have been touched in some way by HCV, that may have something to say about this, and may even be moved by my story. If this could be you please consider adding to the commentary at <br> <a href="http://www.wipo.int/roller/comments/ipisforum/Weblog/theme_seven_how_is_intellectual">http://www.wipo.int/roller/comments/ipisforum/Weblog/theme_seven_how_is_intellectual</a> <br> where I sketch out a personalized (real-life) story of how intellectual property law affected my life, and what I think needs to be done to fix it. If you're moved by this consider also adding to the Wiki at <br> <a href="http://hcvaction.org/">http://hcvaction.org/</a> <br> Also, I think it's important as open-source and serious Ruby programmers for us to defend our intellectual turf from takeover from the noncreatives. Finally, for the value proposition, I submit my work in progress at <br> <a href="http://complearn.org/">http://complearn.org/</a> <br> which is a handy C library for data mining that also has a Ruby binding. So even in purely economic terms, perhaps it makes sense to fund HCV research a bit more. Let's please have this discussion. And have it in more than a few online forums because it's really important to many people right now. And some of these people are people you know. Due to the stigma associated with HCV, most likely you have not yet heard about them unless it is very close family or friends. <p>If you think this issue deserves more attention, or you just want me to continue to be a productive member of the Ruby community for many years to come, then please consider signing the petition above, adding to comments at the WIPO forum, or reposting this or similar information anywhere you can think of to improve visibility to get this desperately needed funding. Usenet, message boards, relevant email lists, anywhere. I cannot tell you how much I would appreciate it if you guys manage to get some real money allocated on this issue from any source. But I will be able to show you, over time. <p>To a better tomorrow, <p>Rudi <a href="http://cilibrar.com/~cilibrar/erblog.cgi/Projects/YouMeAndWipo.html">[more]</a>