Search Results: "rul"

30 November 2005

Erich Schubert: Pyroman V0.1 finished

I just finished rewriting an old firewall tool from Perl to Python. Since it's in Python and about firewalling I dubbed it Pyroman. I investigated a dozen of firewall-tools before, including shorewall and firehol. Each had it's stength and its weaknesses. After writing iptables rules in a shell script every now and then for a more complex project (with like 6 networks of which 3 are bridged together) and two dozen of differently configured hosts, NATs, VPN, everything. My predecessor had written a shell script to configure the firewall, but this was really bad to maintain. So I ended up writing a perl application to generate the rules from a modular configuration (read: usually one file per host, containing a perl hashmap) Well, after happily using this script for two years, I dedcided it's about time to rewrite it and document it extensively. I chose python for the rewrite. You can get the result here: Pyroman 0.1.1. The good: The bad: To tease you a little more into testing, here's an example host configuration: ("dmz" is an interface alias - where the web server is connected to -, as are "INT", "DMZ" and "ANY" for clients on these interfaces)
"""
A really simple webserver configuration.
These examples are just boring... ;-)
But without NAT they would be even more boring. ;-)
"""
# web server
add_host(
        name="web",
        ip="10.100.1.2",
        iface="dmz"
)
# offering, well, web service.
allow(
        client="ANY DMZ INT",
        server="web",
        service="www ssh ping"
)
# internal hosts may access FTP, too
allow(
        client="INT",
        server="web",
        service="ftp"
)
# setup NAT
add_nat(
        client="ANY INT",
        server="web",
        ip="12.34.56.80"
)
(Yes, this is a python script. No, you probably won't care to write your configuration in a programming language, will you?)

29 November 2005

Lars Wirzenius: Travel report: Quest for Sun '05 (Madrid)

Sun! Sunlight is a wonderful thing. Yesterday I came back from a six day vacation in Madrid. The weather was not warm, a bit warmer than in Finland, but especially it was dry and sunny. Pretty good autumn weather, actually. The best week I've had for a while. This was my Quest for Sun '05. I stayed at Amaya's place, slept well and long every night, had a wonderful time with some Debian people, and walked outside and visited a couple of wonderful art museums. Thank you, especially, Amaya, Holger, and Miriam. And also vi and emacs, the feline foot fetishists who rule Amaya's apartment. I took some pictures. They're touristy snapshots, not meant to be art. I meant to make notes about my experiences during the week, but the nice, classy Moleskine notebook I bought especially for the trip got lost the first full day. Some moments stuck into my mind, however. Since the Quest for Sun '05 was a success, I definitely will do it again. A week of light (and sleeping properly) really improved my mood. Maybe even enough that I will be able to deal with the remaining months of dark, gloom, wet, and cold of the winter in Helsinki. The only big negative thing about this trip was flying. I suggest that international air travel henceforth shall be called "The Cattle Drive".

Erich Schubert: SELinux up and running

My "test" boxes (well, they are in fact production systems) are now all up and running SELinux with a "strict" policy and in enforcing mode, after some weeks in "permissive" mode to detect the last missing policy rules (well, maybe I'm still missing something in cron.monthly?) What took most of the time was in fact to write policy for some services or custom applications that didn't have one before. And that I basically was just checking the logs every day to see if some new audit errors had appeared. Oh, and inbetween we completely emptied the server racks and their wiring and redid the room... Just to mention a few things that were "missing": My OpenVPN is running a custom script to update DNS on login and logout, which obviously was missing from the SELinux policy. I'm also using heartbeat to failover between the two firewalls and two mailservers; that policy took me probably one hour (without much previous experience) to write. Then I have another custom LDAP to aliases for a Lotus directory (which hopefully will be replaced by a sane application soon... whoever invented "implicite email adresses" should be shot. Just put all email adresses into the directory, so any app can look them up without trying to guess what your generation rules are... firstname.lastname@domain.tld sounds easy, but what with non-ascii characters?) Anyway, the systems are doing pretty well. Maybe I'm going to enabled SELinux on the web server next. ( (cra-)PHP and typo3 will probably make that more difficult, though...) Oh, and I need to sort out which of my policy changes are local changes, and which I should feed "upstream".

28 November 2005

David Welton: Open source economics - the conundrum

I think I agree with Ian Holsman's analysis of this article on "How to make money from open source" - actually I would go a bit further. Ian says that you don't need to be a committer to make money from open source. Indeed, in some ways the big question in terms of "open source economics" has been how to more tightly couple the people doing the valuable work with some percentage of the rewards - at least enough to let them keep hacking. While it's evident that open source works, and that there are big economic benefits involved, it's a tough nut to crack in terms of renumeration for people working on stuff. Selling software is, if nothing else, a really simple mental model that is very easy to grasp - you write software, you sell it. If it's good, people buy it and you can either reinvest that money, or take it as profit. Open source software that scratches an itch has immediate benefits to the author, especially if the author manages to get other people on board to help out. But the category that's not really covered is the group of people writing something "out of the blue" because they think it's a cool idea. If it is, and it takes off, they really aren't guaranteed anything at all for their troubles. I don't see too many ways around this while still playing by the rules of the game. There are some clever ideas like dual licensing, but they aren't applicable everywhere. I suppose you could declare software a public good and get the government involved in its production, but I'm not very enthusiastic about that... In any case, it's an interesting topic, and perhaps some day clever entrepreneurs will find better ways of financing the production of open source software.

Radu Spineanu: Shoot the movie industry

A couple of years ago i watched a really interesting movie. It was apparently about nothing, but in fact it was rather deep, and not a cliche. The story of two persons, stuck in an unfamiliar place who found each other and started spending time together in this pretty magic place. The action was placed in Tokyo, and the whole idea was based around it's beatiful scenery. The movie is called 'Lost in Translation'.
'.

I really enjoyed the movie, and most of all the two actors. One of them was Bill Murray, and the other one Scarlet Johansson. Scarlet Johansson, even though she was only 18 (i think) when she shot the movie, portreyed a young woman left alone in this unknown universe, who tries to find herself (sounds cheesy, but it's not).

And then came this day, when i had some free time and dediced to see 'The Island'. I have never wanted to kill myself more. Basically ObiWan finds out he's a clone, escapes from the military bunker while in the proccess taking his cloned girlfriend with him, and then frees all the other clones. Also in the movie you can find out that MSN will rule the world, Cisco the telephony, and Puma the shoes industry.

Now about Obiwan's girlfriend who is interpreted by miss Johansson. She is the regular stupid innocent blonde (too many adjectives?), who everyone wants to have sex with. Her role in the movie is probably to look good and have a sex scene with Obi. I mean what the hell ?

The movie thing is probably nothing new under the sun, and i accept the quality of today's movies. I think it was just sad to see somebody i really liked, and thought maybe i would enjoy nice movies with, become another random actor.

Bleah...

David Moreno Garza: Venezuela

I was in Venezuela last week.
 
Something special happened. No, it is not only that I was able to see my beloved girl; it is not that the free software community and free software efforts and project rock. No, something very personal and special.
 
I arrived at Maiquetía (roughly, international airport of Caracas) from Maracaibo around 9 pm. At around 10 or 10:30 I was on my way to the hotel on a van where they pick up and down people on the airport-hotel-airport basis. I was waiting for my early flight to Mexico City the next day (on Sunday, at 8 am).
 
On our way to the hotel, I asked a lady sitting next to me: "So, how far away from Maiquetía is Caracas?". "Around 40 or 45 minutes", she said. Ah, nice, next time I come to Venezuela I definitively should go to Caracas, I thought.
 
"You're Mexican, aren't you?". "Yes", smiling. "I'm going to Mexico tomorrow", she said, also smiling. "Are you in the Mexicana 8 am flight?", I asked, being curious. And she was. We talked a bit more and apparently she was coming also from Maracaibo on my same flight, now that's a coincidence. "It is my first time in Mexico tomorrow", so it was mine in Venezuela.
 
Arriving to the hotel, we checked in and went to rooms. On the morning, at the next day, I found her on the hotel-airport van again. I thougth she was going to be already on the hotel, specially since I was late for almost an hour, but no, she was there. "So we are late, eh?", she asked me. "I think so".
 
After exhausting militar revision (four times, with four different soldiers:  Political relationship between Mexico and Venezuela is not healthy nowadays), I documented my baggage. The lady was in the executive line and I was with mortals. She was pregnat, also. The lady on the desk gave the boarding pass (at that point we were supposed to be already on the gate, one hour before time) and pointed me another desk where I was supposed to pay airport taxes. Now, that is scary, since I was not carrying too much on bills or coins. I arrived to the desk: "how much is it?". "105'000 bolívares", around 50 USD. God dammit, I was only carrying 45'000 and my credit cards are broken. At that sudden time, I was already thinking I should be spending a couple more days to get that fucking cash and pay for the change of flight, etc. Or... I could ask for help to the lady. What do I lose if she says no?
 
- Mrs, I have a real problem, and I need help. Anyway, I will truly understand if you are not able to do it, but I'd much appreciated it.
- What happen? 
- I need to borrow 60'000 Bs from you, please, please. I don't have enough to pay for the taxes and my credit cards are completely broken, I didn't think on taxes when spending money on anything else before.
- Don't worry, let's go to the ATM.
 
There. There I go. That completely unknown nice lady helping me to not lose my flight.
 
- But I have to go first to a Internet center to make a transaction online to be able to get cash from the ATM.
- Yeah, of course, I'll go with you- What else could I do?
 
She took the cash off and went together again to the taxes desk. She gave me the money and we both paid. Only 15 or 20 minutes were left for the flight to departure and we were trying to do things fast. Fortunately, we both did it since more cops and soldiers were making inspections just on the airplane's door.
 
When she was on the line I went to the phone booths to make a call home.
 
- Mom, I need you to bring 300 pesos when you go pick me up to the airport. Long story.
- No problem.
 
And the lady was not there anymore, I was unable to see her again (probably because she was on executive or because she was an angel :-). When we landed, I didn't see her again on the plane, in the lines, in the police line, etc. I have no idea how that happened, but it happened.
 
She will be awarded with twins :)
 
She was wearing a David's shield on her neckless, by the way.
 
 

24 November 2005

Aigars Mahinovs: Cosplay warm-up


Cosplay warm-up
The opening of AnimeFest went great - there were few cosplayers and a huge lot of general public. Anime is on the high.
The fest was opened by two cute girls - one of them was speaking in Latvian and the other in Russian. At first they were translating each others lines both then they diverged and even started a small dispute. You had to know both languages to "get" the joke, but I think most of the people in Latvia do know Russian enough for that.
The opening program consisted of "Howl's Moving Castle" - a truly wonderful anime movie from the same director as the "Spirited Away". A great fairy tail as expected from Studio Ghibly.
Also:

My blog is worth $5,645.40.
How much is your blog worth?

I have no idea why, but I would like to check out half of it, please :)

Martin F. Krafft: Bug squashing bounties

I just sent this to debian-devel-announce. Hopefully it will be a success!
Dear fellow developers and contributors, My publisher, Open Source Press, and I were pondering ways to help the F/OSS community (and Debian in particular) over a beer the other day, and the following idea is the result:
We are announcing a bug squashing period, starting now, and ending 14 Dec 2005, 11:59 CET. Squashing a bug gets you a certain number of points (depending mostly on triviality and severity). At the end of the three weeks, the 25 bug squashers with the highest score shall receive a copy of my book, The Debian System, donated by the publisher. If this turns out to be a success, we'll lather-rinse-repeat sometime soon.
Bug squashing is to be done according to the rules listed on the wiki. In particular, everyone is expected to make proper use of usertags to communicate to others that s/he is working on a particular bug. If you are working on a bug, you are expected to send status updates to the bug report at least once in four days, or someone else may announce the intent to lock the bug and take it over after 24 hours. Fixing a bug also involves augmenting the bug report with any relevant information as you go along, and testing the fix with which you came up.
Points will be given to those whose contributions fix a bug. A bug will be considered fixed if a patch addressing the problem has been sent to the BTS and the package's maintainer has not vetoed the patch within five days. Please do not actually close bugs in the BTS (unless you really know what you are doing). Points will only be awarded to patches for bugs which have been reported before this announcement (but you are, of course, welcome to fix any bug -- if it's obvious you aren't trying to cheat and you are otherwise very actively fixing bugs, you may even get points for those too). After submitting a patch to the BTS, please send a short note to book-bsp t pobox.madduck.net with the bug number in the subject, and from the email address you used to interact with the BTS. In the message, list any contributors who have helped with the bug, as well as an approximation of the time each of you spent on fixing this bug. Also, document how you have tested your patch. The amount of points to be awarded depends on the severity of the bug report, as well as the triviality of the patch you came up with. Put differently: while a one-line patch for a wishlist bug might get you 1 point, I could award 100 points for a clean yet elaborate patch to an important bug. Bonus points can be given if you provide reasonable proof of thorough testing of your fix (you could use test-driven development techniques..., also see here), ideally by a third party. Please be aware that bugs fixed by the reporter him/herself will be especially scrutinised.
Anyway, there are no strict rules for the amount of points a patch will get you. In fact, I reserve the right to announce the scores only after the end of the bug squashing period, and I will not answer questions about scores, or address complaints from people who think they have not received enough. However, I shall act as objectively as possible, fair, and honest; for that, I give you my word. I realise this is sketchy and if you don't trust me on this, it's your choice not to participate -- there is no point to argue at this stage. If you have a better suggestion, I would be interested in trying it the next time 'round. Maybe we can establish a good scoring system for the future, but at the moment, no such system exists. If you have any questions or suggestions, please do not hesitate to speak up (Reply-To set). Do note, however, that my tendonitis forces me to take things slow. For this reason, I would like to apologise in advance should the evaluation of all your contributions extend into the new year. Thanks to Markus Wirtz of Open Source Press for supporting this idea, and to Steve Langasek and Frank Lichtenheld for their advice.

23 November 2005

David Welton: Reading material

Via Ugo Cei I found this reading list: http://www.joelonsoftware.com/articles/FogCreekMBACurriculum.html, some of which looks to be interesting, but I'm not convinced that it's a selection where each book is a 9 or a 10. Books like the Mythical Man Month stand the test of time, and some of the books there are good ones by that measure, others look less convincing, at least judging by their contents (too focused on the 90ies, the bubble, a particular company), or some of the reviews on Amazon. Perhaps those aren't always worthwhile, but the real winners don't seem to have much flack thrown at them. I'd enjoy hearing why Joel et al. recommend them. I've read these: in addition to Microeconomics (6th Edition) as a textbook on Economics. What are your recommendations? All things considered, though, I have a hunch that it would be better to be out trying to *do* rather than reading books about doing...

Antti-Juhani Kaijanaho: The Finnish Criminal Court System

(Reposted from old blog) Inspired by Matthew Palmer’s writings (1, 2) on his experiences about the Australian judicial system, I decided to write a series of essays on the Finnish legal system, mostly to contrast it with the universally familiar United State system (even if that universal familiarity is a little skewed by generally having been obtained from dramatizations and not actual court practice). I will concentrate on criminal cases. I will write based on my personal experience as a part of the judicial system, and I will be supplementing the experience with my personal research into law. I am not legally trained, but I have read a fair bit of law and related literature. I am a lay judge in the Jyv skyl District court. I will explain the significance of that later, but I hope it suffices to say, here, that I have sat on the bench, as part of four-judge panels, since March this year, in ten sessions each a day long, and each containing at least two, usually four or five criminal cases. Some highlights: Later essays in this series: Fictional depictions of court cases in this series: Read more

Gunnar Wolf: My Perl name

Thanks to Algernon, I also discovered my true Perl name:
''=~('(? '.('^/@@[ '^'.])./]').'"'.(':^@@]/ /,;_'^' +..<]]*@@] ').',$/ )')
Isn't it crystal-clear?
...Seems I'll have to repeate Algernon's hint: Perl, enter, paste, Ctrl-D.

Antti-Juhani Kaijanaho: The European Union has a bicameral parliament

I claimed yesterday that the council of minister looks very much like an upper house of a bicameral parliament. It is somewhat unusual in that the members are not chosen for a certain term but serve by virtue of holding an office in a member state, but it is not a decisive unusuality. Martin-Éric Racine countered: No, it is not. The council is bureaucracy overruling the Parliament, thus invalidating the very principle of democracy. We have seen this in action with the new EU Patents law: the Parliament had passed several amendments to the original proposal, yet the Council ended up ignoring those democraticly voted amendments and passing an even worse legislation than the original draft. This is clearly different than what happens between a higher and lower chambers each examining new legislation before it is ratified. Wouter Verhelst already said most of what I would have said myself; the bottom line is that Martin-Éric is mistaken about the powers of the council of ministers. Does he by any chance confuse the council of ministers and the commission? It would explain his calling the council a "bureaucracy", which it really is not. The power balance between houses in bicameral parliaments varies. My claim cannot be dismissed just because the two organs do not work exactly like the English parliament, for example. An important thing is that the lower house is almost always directly elected and the upper house comprises often representatives of areas or member states. Finally, I would like to note that the bicamerality of the EU parliament system is independent of my claim that the EU is a country; if a non-country can have a parliament, it certainly can have a bicameral parliament. So you don't have to rebut it just because you don't believe in my claim about the countriness of EU. Oh, BTW, Martin-Éric, please don't mess with your posting dates. It makes your postings to appear more recent in PD than they actually are and to float to the top, where they obscure legitimately new postings by others. For example, your response to my previous posting appears much later in PD than Wouter's reply to it.

Antti-Juhani Kaijanaho: On the European Union Constitution

Jordi Mallach expressed fear, uncertainty and doubt about the European Union Consitution. I would like to make the following two points:
  1. Much of what is in the proposed Constitution is already in force as part of the current foundational treaties. So, if you read the Constitution and want to bash it, please read also the current treaties. I expect you to find it to be clearer and content-wise quite similar to them. So don't think that this document is an evil attempt to seize power or something (well, it might be that but I doubt it).
  2. A change to the current foundational treaties would be necessary in any case. The acceptance of new member states makes decision-making by the current rules difficult, if not impossible.
I assume most of the FUD spring from the grand name of the new document, a Constitution. It deserves the name quite rightly. It is the sole foundational document after it is accepted, replacing quite a few treaties. However, its impact is mostly illusory. A similar change was done in Finland a few years ago, when the three constitutional statutes (none of them called the Constitution) were combined and updated into a single law called the Constitution. (Long-time Debian developers probably remember my ill-conceived ITP from four years ago:) Perhaps this FUD is exactly why the consitution should not be subjected to referenda. I don't doubt that Jordi is not alone in his feelings, and such feelings could easily cause it to fail in a referendum. The only effect of such a failure would be making decision-making in the EU painful, without actually addressing the issues people have, such as Jordi's regional issues.

Gergely Nagy: My Perl Name

''=~('(? '.('[_@@*@'^'+-).^ ').'"'.('>,<%_@/@ '^'_@[@-.@.^').',$/ )')
Generate your own perl name! Sick, sick, sick, but I love it. Hint: run perl, paste the code, and hit ctrl+d

22 November 2005

Erich Schubert: More SELinux backporting

I've found a couple of issures with my SELinux backports. One is actually a bug present in sysvinit and cron (at least): The debian/rules makefiles uses dpkg-architecture to detect a Linux system (as opposed to GNU hurd or FreeBSD or Solaris) since SELinux only works on Linux. This code queries the DEB_HOST_ARCH_OS variable via dpkg-architecture, but this variable isn't available on sarge (yet)... When building with either my dpkg backport or by using code similar to the following:
dpkg-architecture -qDEB_HOST_ARCH_OS   dpkg-architecture -qDEB_HOST_GNU_SYSTEM
the packages will actually have SELinux support enabled. I've already filed a bug against sysvinit, but should I also file bugs against logrotate and cron, who apparently use the same code to detect SELinux? (openssh and coreutils have better fallback code than suggested above in place)

Edd Dumbill: Opinions and opinionated software

Since interviewing the creator of Ruby on Rails, David Heinemeier Hansson, I've been thinking a little more about what he calls "opinionated software." That handy little phrase seems to sum up what attracts me to a lot of technology, at all levels. What I like Free software--it's not really economics that drew me to free software, but the principles and aims. When you release or use free software, you're exercising your right to free speech and adding your voice to an argument. Desktop environments--GNOME 2.0: The move from GNOME 1.0 to version 2.0 was highly opinionated (Mac users, think OS 9 to OS X). Complexity of user interface was thrown out in favour of sensible defaults. Web standards--CSS, RDF, XHTML: not only is there a right way to do things, but it turns out to be better too! I don't automatically fall in love with everything that emerges from the W3C, but some things are so obviously the right way it's impossible not to love them--warts and all. Muine music player--this player throws out the paraphernalia of iTunes clones and gets you close to the music. Queue, play. What I want to do. Epiphany web browser--a simple text bar does for bookmark lookup, search and URLs. The bookmarks work like del.icio.us tags. Despite the lure of Firefox extensions, simplicity and efficiency keeps me with Epiphany. Make friends and enemies The thing about opinions is that people are inclined to disagree with them. Opinions won't please everybody and, being opinionated, you will never carry everybody with you. But opinions give you something with which to scythe through indifference. In both my writing and software development work taking a particular stance enables me to create interest, and gives direction to my projects. I tend to get bored easily, and if I'm bored, then my readers and users will never make it. Being opinionated in software development is easier when there's a strong leader, or it's a one-person project. It's more of a dilemma if you're running a free software project with multiple contributors. Such projects need such contributions, but run a risk of losing focus and style. I think that there we need to be brave again. If being opinionated means losing potential users, it also means losing potential contributors. Opinions help in dealing with users, too. It's obvious that the mere act of listening to users isn't good enough. They often won't say what they mean, or their requests mask a deeper issue. How often have you given your boss exactly what he asked for, to find it wasn't what he wanted? A combination of reading-between-the-lines and having opinions helps cut a way through the noise of user feedback. Stirring the pot Here are a few wishes for some free software projects that I want to see succeed further, but which really need to get opinionated to be effective. These projects aren't failing, but they're not reaching the potential I know is there. Each of them are infrastructural in some way, and that's probably where the temptation to avoid being opinionated is strongest. But here's the genius: the rewards of being opinionated here are high. Ruby on Rails is the existence proof. What's better? A handful of users with the "right" idea about how fundamentally important you are to everything, or loads of users who only see a portion of the capabilities of a project? Copy the experts It may seem like cheating to copy Apple's lead, but a lot of what they've done in OS X is to take a stand on what's useful and not. On many occasions this has inconvenienced people, but it's not eroded that special lure that Apple products hold. There's not harm in asking "What have Apple done?" with some of our projects. Another poster-child for being opinionated is Ubuntu Linux. Technically, it's only marginally better than other Linux distributions. Strategically, it's miles ahead. Its founder, Mark Shuttleworth, had very solid opinions about where Ubuntu should go, and pursued them. Over time, these have pervaded the product itself, but at the start it was the message and the style that made the difference. Opinions not expressed are rarely effective.

21 November 2005

Sam Hocevar: Finding a specific instance of a given day in the month in PHP

Andrew: to work out the fourth Thursday of the month, use this instead:
function nth_day($instance, $dow, $month, $year)

  $first_dom = date("w", mktime(0, 0, 0, $month, 1, $year));
  return 1 + ((7 + $dow - $first_dom) % 7) + 7 * ($instance - 1);
Or, using Zeller s rule and avoiding function calls:
function nth_day($instance, $dow, $month, $year)

  if($month < 3) $year--; $D = $year % 100; $C = (int)($year / 100); $m = 1 + (($month + 9) % 12);
  $f = 1 + (int)((13 * $m - 1) / 5) + $D + (int)($D / 4) + (int)($C / 4) - 2 * $C;
  $first_dom = $f % 7;
  return 1 + ((7 + $dow - $first_dom) % 7) + 7 * ($instance - 1);
Update: thanks to Andrew for spotting the error in the second version. Due to my misunderstanding of a hack in Zeller s rule, I had forgotten to decrease the year when the month was January or February. Also, PHP was not doing proper integer divisions and I had to cast results back to int despite all operands being ints.

20 November 2005

Matthew Palmer: The Fun Never Stops With Ruby

Well, there's nothing like posting a really rubbish program to your blog to find out the right way to do it. Firstly, Philipp Kern has come up with another, even shorter version than his version I posted in the update to my original Ruby post, this time blowing me away with the strangeness of explicit casts in Ruby. Apparently, casting an input stream to an array produces an array with one-line-per-element, as in this example:
 STDIN.to_a.sort_by rand .each l puts l 
As Philipp warns, however: "Ruby is full of fun and power; one must watch out that the scripts remain readable". For a fairly toy example, this solution is still fairly readable, however I can see hair growing on this thing if your program grows large. Next, Pierre-Charles David showed his qualifications for becoming a co-author of the next edition of the Pickaxe (AKA "Programming Ruby: The Pragmatic Programmers' Guide"), with his example and explanation. He's kindly given permission for me to reproduce his e-mail in it's entireity, since it's too good to chop up:
Here is the shortest and most readable version of your "randomize stdin" script I can think of:
  #!/usr/bin/env ruby
  print ARGF.readlines.sort_by   rand  
 
ARGF is a special variable representing either stdin or, in the case where file names are passed on the command line, the content of these files (think of Perl's <>). ARGF behaves like an IO object (although I think it is not one). In particular, it responds to the #readlines method which splits the receiver's content into lines and returns an array. In other words,
  lines = ARGF.readlines
 
is the idiomatic way of doing
  lines = []
  while line = gets() do
    lines << line
  end
 
in your example. The next part (sort_by) uses a new form of the sort method, which I think is not documented in the Pickaxe. It implements the Schwartzian transform technique. In essence, a random number is associated to each element of the array, which are then sorted based on this index. As a general style remark, note that although Ruby supports "while" and "for x in y" loops, they are rarely used, in favor of iterators and/or higher order methods. For example, to gather all the input lines in an array without using readlines, one might write
  lines = Array.new
  ARGF.each_line    l  lines << l  
 
or
  lines = ARGF.inject([])    lines, l  lines << l  
 
Hope it helps, and welcome to Ruby!
Pierre, your explanation definitely helped, and I hope it helps others too. The third person to help me out is Decklin Foster, through a post in his blog. What particularly caught me about his solution was the neat demonstration of another feature of Ruby which I think I will really learn to love: the ability to dynamically add methods to an existing class. "Aiee!" I hear all the purists scream -- and I can see the potential for severe misuse, too. But, since every good idea seems to get deeply perverted, we may as well have some real useful features to pervert while we're at it. Decklin has the clever idea of adding methods to the Array class that shuffle the elements of the array in a random fashion. His code is beautiful in it's simplicity:
 class Array
    def swap!(a, b)
        self[a], self[b] = self[b], self[a]
        self
    end
    def shuffle!
        each_index do  i 
            j = i + rand(length - i)
            swap!(i, j)
        end
        self
    end
 end
He then goes on to rewrite my bumbling example in probably the most comprehensible manner I've yet seen:
 STDIN.readlines.shuffle!.each    line  puts line  
Decklin writes, "Now that is cool.". I couldn't have put it better myself. My thanks to Philipp, Pierre-Charles, and Decklin, for helping me along the Ruby path. So, two new Ruby Rules for me to live by: Hmm, is it possible that Ruby is nothing more than Functional Programming by Stealth? Aiee!

19 November 2005

Aigars Mahinovs: Little tiny side note: F-Spot rules as a photo org...

Little tiny side note: F-Spot rules as a photo organiser: I can import my photos from the camera, tag them, print them, export them too photo albums, BUT there are two tiny issues:
* why cann't I start GIMP on a photo and automatically get a new Version of it when I save (preferable a separate version for each save operation)? or why can't I just say that photo 1 is a version of photo 2?
* why exactly does F-Spot consume over 250 Mb or real nonshared memory during photo import from camera and almost 150 Mb of memory at all other times?!?!?!!?!

grumble, grumble, grumble, must file bug reports, grumble, grumble...

18 November 2005

Aigars Mahinovs: Real fotoday


Creepy windows
Today was the Independence day of Latvia. I spent most of it walking around the city of Riga with a camera and taking pictures. The weather was fantastic (except it was cold and icy) the colors that spread across the sky and people were truly amazing. I took lots and lots of pictures and managed to select 10 of them to upload to flickr for others to enjoy. Unfortunately none of 150 pictures I took during the fireworks are good enough for me to dare showing them around - the fireworks were kind of weak this year. Maybe next time.
So - I hope you'll enjoy looking at those pictures at least as much as I enjoyed taking them.

Next.

Previous.