Satya's blog - 2008/

Nov 24 2008 10:25 Rails: Override Table Name Quoting

We had a situation where our Sybase database didn't qppreciate that Ruby on Rails was referring to table names like so: [table_name]. The square brackets are okay in Sybase but our particular database was too old for it. So, I have to override table name quoting. In my Rails app, in config/initalizers/sybase_adapter.rb, I place this code:

# sybase_adapter.rb
# stolen from rails' to override the table name quoting. []-quoting throws off atlas server.

module ActiveRecord
  module ConnectionAdapters
    class SybaseAdapter < AbstractAdapter # :nodoc:
      def quote_column_name(name)
        # If column name is close to max length, skip the quotes, since they
        # seem to count as part of the length.
          name
        #((name.to_s.length + 2) <= table_alias_length) ? "[#{name}]" : name.to_s
      end

    end # class SybaseAdapter
  end # module ConnectionAdapters
end # module ActiveRecord

And it works. Table and column names are no longer quoted. Much thanks to folks on the #rubyonrails freenode IRC channel. Specific users: %w[ blj Bobnation defswork jammanbo rsl ].join(', ').

Last updated: Nov 24 2008 10:34

Tag: geeky rails

Nov 20 2008 20:27 India declares War on Piracy

A BBC article about piracy in the Gulf of Aden says one-sixth of the world's sailors are Indian. This makes Indian sailors a big target for pirates. A lot of Indian shipping goes through the Gulf of Aden, apparently.

Cool. Real pirates suck.

Update: The "Cool" part was that the Indian Navy is going up against the pirates. At least one ship is patrolling the area, according to the BBC article

Last updated: Nov 21 2008 13:22

Tag: news

Nov 19 2008 08:02 PHP and Ruby on Rails compared

I received an email asking about differences between PHP and Rails. This is my reply. It has been cleaned up a little. It is not comprehensive.

Both PHP and Rails address the same domain: making dynamic web sites, in different ways. PHP, at heart,is a language that embeds itself in your HTML document. Now, it's OOP-ness and other things (such as template libraries) make it much more than that to the point that people forget what it is, but still. In Rails, the HTML page generation happens at the end of the request-response cycle....

Both are pretty newb friendly. I believe it's easier to write bad programs in PHP than in Ruby, though.

At this point I'd like to point out that Ruby is a programming language (notice I didn't say "web") and Rails is a web programming framework (notice I *did* say "web"). I point this out because people at work are always confused. And in the above paragraph, I compare PHP with *Ruby*, not Rails. When you build a RoR web site, a lot of the programming you do is in Ruby, hence the comparison.

As for function and speed: Rails, being Web-oriented in a Web 2.0 world (that means AJAX and dynamic HTML tricks are the norm rather than an exception -- and they're not to be used gratuitously, but only where needed -- and since many browsers are in Web 1.5 (I make that up) or earlier, AJAX should not be a necessity, which means it should be added on, not be the primary way to do things. As a general guideline, which I freely violate in controlled conditions. Professional programmer, closed subnet. Do not attempt.)

Ahem. So Rails has full and easy AJAX support, which means AJAX-y things are easy to do. You might be able to find code libraries in PHP, I don't know. Rails makes it really easy, though. It uses the MVC (Model View Controller) paradigm, and the View part is usually HTML, but it could be Javascript or XML, too. That's where you get half the AJAX functionalitiy.

(Not in the original email:) Rails includes the prototype and scriptaculous javascript libraries, which make AJAX and DHTML tricks very easy.

You may not need to do much AJAX anyway. I see your question stems from trying to speed up a potentially slow Rails app. Where did you hear that Rails is slower than PHP? It could be, but let's examine the bottleneck areas and comparison points:

PHP usually runs as a module of your web server (Apache). That makes the PHP interpreter pretty fast. Rails usually runs off mongrel or something else, which Apache has to proxy to. Apache is a decent web server, having it in front of your Rails server is a good idea. You can remove this bottleneck by running multiple mongrel processes for your Rails app, and have Apache do a load-balancing proxy. This is quite easy to do. Apache config:

    <Proxy balancer://yourapp_cluster>
        BalancerMember http://127.0.0.1:8290
        BalancerMember http://127.0.0.1:8291
        BalancerMember http://127.0.0.1:8292
        Order Deny,Allow
        Allow from all
    </Proxy>
    <Location /yourapp>
        RewriteEngine On
        RewriteRule ^/(.*)$ balancer://yourapp_cluster%{REQUEST_URI} [P,QSA]
    </Location>

(Mongrel listens on the ports in the BalancerMember lines.)

Now, mongrel does take memory, being a separate process. It's not so bad, though. You don't need huge resources, but I wouldn't run a Rails app, or much more than 1 or 2, off a virtual machine.

Speed of actual code: I can't compare. Of course, if you write an O(N^2) sort algorithm, you're screwed either way. (Hint: both PHP and Ruby provide sort functions, but this is only one example).

Database connection: This is likely to be the worst issue. Rails presents your database (DB) in an object-oriented manner, called Object-Relation Mapping (ORM). An ORM is usually pretty chatty with the DB, always asking for table descriptions and so forth. A lot of that can be optimized away, if you're reasonably careful about indexes, eager loading, and how you do your algorithms. Judicious use of periodic database maintenance scripts can help, too, by generating cache tables.

You're trying to optimize prematurely, as you've guessed. I believe Rails can support a pretty busy PBBG, depending on how busy each page is going to be.

Rails is used best on a server where you have shell access. You can set up a development environment on your Windows box, but you'll need a database. MySQL is recommended. For actual use, i.e. production, I recommend a Linux server, Debian preferred, where you have root access. Linode and csoft can give you virtual machines. Despite my earlier injunction against one, initially you can use a VM. If it gets busy, you can switch to dedicated hosting -- if you can make money off it.

Rails development happens with it's built-in web server, Webrick. Once you start it, (./script/server) you connect to http://localhost:3000/ . That's localhost port 3000. Do NOT use that for production.

Oh, if you want to try Linux for a home/development machine, grab an Ubuntu LiveCD: https://help.ubuntu.com/community/LiveCD

Last updated: Nov 20 2008 20:36

Tag: geeky rails

Nov 16 2008 13:50 Text on web sites

Jakob Nielsen says "In real life, users often suffer under tiny text and websites that add insult to injury by not letting users resize the words."

That leads me to an annoyance: some corporate web sites have a "text resize" link, usually accompanied by a "+/-" or "Bigger font/smaller font" links. These are usually Javascript triggers to resize the text. The annoying thing is, that's not the web site's job!! That's your web browser's job, to resize text to your liking. Most web browsers have this function.

The annoyance becomes a pain when a "customer" requests the same function on a web site I make, because "BigCompany does it why can't you." It's not that I can't. I *won't*. You hired a systems analyst, not a code monkey.

Tag: geeky rant

Nov 11 2008 20:09 Windows XP re-install

Today I re-installed Windows XP.

The windows box has 2 drives, a 15GB with Windows ME and Windows XP on it, and an 80 GB data drive. The 15GB drive failed. So I booted an Ubuntu Linux live CD to run tests on the drive. It seemed to have read errors, so goodbye.

I used the Live CD to move whatever I could, what little was left, onto the data drive. The data drive had 4 partitions, 20GB each, I emptied one of them and crammed the other ones full. "Move over in the lifeboat, folks."

I tried re-installing XP (which involves inserting the ME disk at one point to prove that no, really, I am entitled to the XP upgrade edition) on the 15GB drive. It took forever to format it, and eventually it hung at the keyboard selection screen. I realised later that this was probably because Windows, for some reason, loses it completely when the keyboard/mouse (PS/2, both) go away and come back. I don't know why.

I think the disk may have started failing when I cleaned the computer a few weeks ago. I found the IDE cable was loose. Contributing factor? Maybe. Besides, it's seven years old. Time to move on.

So I disconnect it and reboot a few times, always coming back to "No hard drive found" after a long time of the BIOS poking around. Eventually I realised that changing Hard Drive 1 from "AUTO" to "OFF" worked much better. (Hard Drive 0 is the first one.) Oh, and it helped to put the former data drive on primary instead of secondary. Being an OEM drive, it had NO instructions on it. Usually the jumper instructions are on the drive itself. I bet they were under that cover... but I realised that it was probably on Cable Select when I bought it, and I mus have left it that way, so I just plugged it into the master interface. That worked.

And then Windows refused to install into the first partition, the one I had emptied out. Why? Because the whole disk was one big "Extended" partition with 4 "Logical" ones inside it. Windows refuses to install into a logical partition. Yay. Luckily it had 5GB free, unassigned space at the beginning. and I convinced the Ubuntu Live CD (yay!) to create a couple of random partitions (5GB and the 20GB that got shot up while trying to install Windows in it).

So eventually Windows was installed in a cramped 5GB partition. Windows updates will probably fill it up. Don't care. Remember, my primary computer is Ubuntu; I use the Windows box for games.

Now I just have to get the updates, service packs, etc. I already installed Firefox, Ad-aware, and zonealarm. AVG anti-virus refuses to install until at least service pack 2. But I can play Septerra Core, which was the objective. My newest save game file survived the crash, too.

Tag: geeky

Oct 17 2008 18:13 Why I don't like Exchange
Here's my biggest complaint about MS Exchange: If I'm not using Outlook on Windows, if I'm using, say, mutt on Linux (as I do), I'm forever relegated to being a second-class user, told things like "Oh, it doesn't work for you? You're using that crazy Linux thing aren't you" "Just use Outlook" etc.

Tag: rant geeky

Sep 05 2008 14:47 New Model M keybaord

I got a new Model M keyboard from eBay. FedEx just delivered the box inside a plastic bag -- go FedEx! -- on account of the storm. These keyboards are so ... what's the opposite of fragile? They're so durable, it was packed in the carboard box with just a cardboard frame and no other packing material (besides the shrinkwrap -- original, I think). Normally this means minus a few points on eBay, but like I said, they're very durable.

Unfortunately this isn't a "real" Model M. It's made in 1997 and the part number is 42H1292. It's still the buckling spring design, though :-)

And it came with a mouse. An IBM-logo'd, 2-button mouse with no scrollwheel.

Tag: geeky

Aug 09 2008 16:28 BBC uses same stock photo

The BBC used the same stock photo (of a guy with a laptop) in two articles recently. Both are in the Technology section.

To be fair, it's probably okay to use stock photos like that. It's not wrong. At best, it's funny.

I've ranted about irrelevant photos in news columns before.

Last updated: Aug 09 2008 16:33

Jul 24 2008 10:38 Houses and computers

People don't seem to get that computers are complicated. Insanely complicated. More complicated than any otehr machine they have ever used. Think TVs and VCRs are complicated? Simple, very simple. Think cars are complicated? Not as much as a desktop computer.

A co-worker used to work construction, but now does computer stuff. (Yeah, "stuff". Most people reading this would understand if I said what he does, but I'm not going to. It's work stuff.) So today I'm going to compare the complexity of building a house (a subject I know little about) with the complexity of a computer (something I know a lot more about, but by no means do I know everything.)

A house. What've you got? Walls, floors, roofs, studs, joists, beams, electrical, plumbing, *planning* the thing in the form of an architectural ... er, plan? Permits, zoning, and so on. Figuring out what's wanted, designing the thing. A garage? 1 or 2 car? Or are we talking condos/apartments/townhouses? (Servers, versus desktops.)

What color? Well, that's fairly superficial. A big job, but still can be done after the whole thing is finished and lived in for a few years. That's like asking to change the color scheme of a web site -- can be a pain, but doesn't usually involve a rebuild.

Oh, you wanted brick with aluminium siding? Not the concrete/wood with wood siding we've done, that you hinted at? And when we said "okay, we'll do concrete with wood" you said "yeah whatever I don't know much about those house things", and then you signed off on it? So you want us to change the basic structure of the thing, expect it to be done in 2 days while your in-laws are visiting, and expect it to be free/cheap because we "know about that house stuff"?

That's like asking for your PHP site to be designed in MySQL (real computer people are cringing now) when, in the beginning, you didn't care what we did as long as we did it in a week and it had "social networking, videos, email, and a forum blog". We threw together something slick in Wordpress, you liked it, but now you realise what you really wanted was a forum with a document sharing area. Tough noogies. And the weeks of meetings that you spent deciding on the exact shade of blue in the footer (or, the exact pattern of the crown molding)? Worthless.

Now say you want the kitchen at the other end of the house. Sure, we can do that. Oh, the laundry room too? So you want us to reroute the plumbing and the electricals (the 3-phase supply for the dryer), again in 2 days and cheap? Suuuure. That's the same as when you ask us if the site can have fine-grained permissions bolted on later. And it should support frames (What the hell? You're not qualified to decide that. That's like you asking for the house to be built with 1 room on the 1st floor, 2 on the 2nd, 3 on the third, and so on. It's stupid. It might work in some special case, but generally it's stupid.)

Now, about the materials and construction. Do you realise that the video card in your desktop has more components than your entire house? How many in your house? Let's count every sheet of wood, every 2x4, every pipe and fixture. Let's not, actually. Take a pipe. What does it take to build one? Does every inch of it have to be designed individually? No, you probably roll some steel, thread the ends, and voila, your basic pipe (again, my knowledge of manufacturing fails me). The video card? Yeah, okay, so you can take some standard components and fit them together. I'll give you that. I know it's not true, but fine, whatever.

Now, when you have a leak. Most times you can see it -- there's water on the floor, or whatever. Or your faucet doesn't. A memory leak? What do you get? Random crashes. The application hangs. If you're lucky, it'll be consistent. Otherwise, someone has to go in with a memory debugger or leak tester (in the case of the water pipe -- it's an acoustic device, I think). And believe me, the debugger is a lot harder to operate.

Take tools. When the carpenter re-uses his (or her. I'm not going there.) hammer on various projects, do you scream copyright violation, intellectual property violation, and so on? Does the plumber have to sign a non-disclosure agreement? Can your electrician use the experience gained on his next project? Sigh.

Back to trouble-shooting, which was supposed to be the point of this article. Something goes wrong with your house (computer). Do you call the police (helpdesk) and yell "help it doesn't work"? For that matter, do they say "try re-inserting the key (rebooting)"? Okay, so they say "what's the problem"? and you say "I can't use my GE (Microsoft)", meaning your fridge (word processor). Okay, why? In cvase of the fridge, it could be any number of things, from a stuck door to leaks in the freon. In case of your computer, it could be anything, from the keyboard down to the RAM. In both cases it could be user error or power failure. It could be a corrupt file or mold in the vents. Point is, in case of your applicance there are much fewer points of failure.

If it's a web site or web application I've built, the points of failure are much more. I can replace the RAM, but any number of lines of code could be bad in the web app -- and I have to find out where the errors are. I can't replace the whole app, can I?

I seem to have lost my point somewhere.

Update July 30 2008: I managed to put this more briefly on chat today: Fact is computers are complicated and need maintenance. You take your car in for an oil change every 6 months? Computers are 2 orders of magnitude (200 times for the proles) more complicated. That means an "oil change" every day, and a complete overhaul every 6 months.

Last updated: Jul 30 2008 10:38

Tag: geeky

Jun 28 2008 10:38 Solar electricity and other schemes

I recently posted this on Usenet:

ObHarebrainedIdea: Why not use sunlight focussed through huge magnifying glasses to raise steam for power generation? Or at least help in raising boiler temperature?

Someone responded with a BBC article about a solar thermal power plant near Seville, Spain, which uses mirrors.

To which I said:

Same principle: rather than cover the whole planet in solar cells (sails?) in an effort to capture more energy, concentrate it in a smaller region.

If only I could figure out how to harness the temperature difference betwen outside air and the inside my parked car. I know about Stirling engines, but I need a device that:

a) I can buy at Walmart or equivalent
b) I can plug into an outlet in the house and it will provide power, the rest being made up by the grid. Or, put it between wall outlet and consumer device at least.
c) Other end of device can draw power from solar, Stirling engine, hydroelectric source (rain tank atop roof, e.g.), stationary bicycle, etc.

Tag: idea

Jun 22 2008 15:36 Baked egg casserole

Today I made a baked egg casserole based on recipes I found on the internet. We need this sort of thing for the every-two-months team meetings.

From the other recipes, I found that a ratio of about 1 cup milk to 5 eggs works, and that's the basic idea of baking eggs. That the quantity I used, which feeds 2 or 3, but you can multiply that for more people.

You need cheese. Sliced or shredded will do. I used kraft cheese slices, torn into strips. A baking pan large enough to hold all the ingredients to a depth of about 2 inches is required. I used one that's about 5 by 9 by 2.5. Inches, because this is cooking. If it were chemistry I'd be using more precise quantities in SI units.

You can throw in whatever else you have. I used 2 slices of turkey (torn into small pieces), a pile of green peas, and a veggie burger patty (again, torn into pieces). Cook all this stuff separately before starting anything else. This is the "filling".

Spray pan with cooking spray (or butter, whatever it takes to keep stuff from sticking).

Add about half the cheese, maybe enough to cover about three-quarters of the bottom surface, if you're using slices. Pour in the filling, and put another layer of cheese. If using shredded cheese, sprinkle it in about the same quantity (estimate! this is cooking, not chemistry -- the difference being the precision of measurement, and you can eat the result if done right).

Now mix the eggs with the milk (or vice-versa) and blend. We're looking for uniformity more than anything else. Pour into the pan.

Put pan on baking sheet (I used aluminium foil). Don't bother covering, or cover it if you prefer. I don't know what that does. Bake at 300F for about 1 and a quarter hour (cooking times vary with oven and size of the pan). Make sure it's solidified. Cool it a little, and eat.

Tag: recipe howto