Ruby Dup vs Clone

| Comments

This was written way before StackOverflow launched, and certainly much before it became popular. Those were the days when programmers had find out answers themselves ;) This has since been asked and answered on StackOverflow.

I have had this question about the difference between Ruby’s clone and dup method multiple times now. And I have always had this fuzzy feeling that “they are almost the same thing”. Today my colleague asked it again and I decided to dig it out. Here’s what Ruby API docs say about them:


Produces a shallow copy of obj – the instance variables of obj are copied, but not the objects they reference. Copies the frozen and tainted state of obj.


Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. dup copies the tainted state of obj. See also the discussion under Object#clone. In general, clone and dup may have different semantics in descendant classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendant object to create the new instance.

This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy method of the class.

So in essence, both produce shallow-copies of the object. clone copies the frozen and tainted state while dup copies only the tainted state. Even after reading that documentation twice, it’s still murky in my head (probably coz I have not had a chance to write code to notice the frozen or tainted state specifically.)

Here’s a gem from ruby-talk that Google brought up. So Matz says:

clone copies everything; internal state, singleton methods, etc. dup copies object contents only (plus taintness status).

And so I wrote a bit of code to see it myself (and thus remember it). Here’s a very simple Person class:

Now we instantiate a Person, p and add a singleton method to it to say hi:

See that it’s all working:

Now create a clone and a dup of it:

Both the clone and the dup have the same instance variables (which are shallow-copies BTW), but only the clone has the singleton method (#say_hi) in it:

So that makes me feel that #clone has to do more work than #dup does. Hence #dup might be more beneficial for performance compared to #clone (unless you really need to use clone). Comments are welcome!

GNU Screen

| Comments

If you have nothing to do with Linux you probably can stop reading now. Unless you want to get introduced to a very nice tool in the Linux world. I can say without doubt that this saves me at least an hour every month. In addition it gets you away from doing some mundane “set-up” like things every time to start working. It has a load of features but today I’ll just scratch the surface. But it’ll be enough to start you off and I promise you’ll be glad you found screen.

One of the programs that I love and can’t go through the day without is GNU Screen. It’s like a Window manager, but only for your terminals. You know, like you are working with so many terminals/consoles/terminal-windows all the time (one to the DB, one with vim open, one to run the tests, one to run the program etc., one to watch the logs, a few to prod boxes etc.). And then you disconnect and then reconnect (from home, somewhere else) and you have to set-up all that again?

Yeah screen can help you with all that. My work pattern involves connecting to a screen session (and I get all the windows/context loaded). Whether I connect from work, home from somewhere else, all I have to say is:

$ screen -ls (to see what sessions I have running)
$ screen -R some-session-name

And it’s wicked-easy to switch between different windows, search through your history etc. You can also configure it to do a few “start-up” things every time. For example, at home, I have screen

  1. configured to Open 4 windows when it starts up and
  2. have window-0 go to my code dir (to teh root of my RubyOnRails part of the site)
  3. start up auto-test on window-1
  4. show the logs on window-2
  5. ssh into the blue host (which hosts my site)

Since the windows are persisted, I don’t have to do all these things every time I connect (for a lazy programmer (and all programmers are lazy aren’t they?) that’s a lot of help). You can even share the screens with someone else (I have not tried that yet though).

Anyway, I hope you are sold by now. Even if you are not, spend some time (shouldn’t be more than a couple of hours) installing/configuring/running screen and you will soon reap the rewards. To get you started:

A word about configuring: heard that Ubuntu will soon shipping with decent .screenrc (sane default configuration) so you don’t have to. Meanwhile you can configure it yourself (as in the Red Hat guide above) or look at these cool options: Here is my .screenrc:

vbell off
vbell_msg "Ring"

# detach on hangup
autodetach on

# don't display copyright page
startup_message off

# scroll back
defscrollback 1000

# setup the caption
hardstatus alwayslastline "%{-b gk}%-w%{+b kg}%50>%n %t%{-b gk}%+w %=%C%< "

# right/left bindings
bindkey "^[[c" next
bindkey "^[[d" prev
bindkey "^[[b" focus

# Set the altscreen so that when you quit vi, it will go back to
# what it was before
altscreen on

And finally, my delicious links for screen.

Happy “screen”-ing :)

Where Is the Quality in E-commerce?

| Comments

I have been buying most of my stuff (on the net) from Amazon ever since I moved to the US, in 2006. That’s probably why this thought/blog is coming now, after 3 years.

It’s about the quality, mostly about the customer experience in e-commerce stores. First, I have to say kudos to the handful of places I have had a good experience in – Amazon (and endless, AmazonFresh, windowshop etc.), eBay, zappos, BN and Netflix. These places make it easy for the customer – easy to choose, to place an order and easy to track or follow in case there is a need. It’s (most of the time) easy to use them (categorization, search, find more related info, edit orders, cancel, return – the whole deal).

However, over the last one week, I have had a “chance” to use a few other sites. Three of these were for techie/geeky t-shirts (which you won’t find, easily, in the aforementioned stores) – CafePress, The Ubuntu US Merchandise Shop and Zazzle (for Mozilla/Firefox merchandise). Maybe, I am spoilt by the bigger, better sites – but I did not find it easy to search for things, find more info on them on these sites. They probably did fine, but not great. However, the experience after placing the order is not nice at all.

The email I received from Ubuntu informing me that my order is complete does not have a single hyper-link. It does not even have a order number. It has the long UPS tracking number in it, but is it so hard to embed a link? And where do I go to see the details of my order? No link, no order number. They don’t have a picture of the T-shirt in the e-mail, so if I want to look it up to show to someone, I have to go to their site (the US one by the way, is harder to find). But if I search for the item by the name they sent in the e-mail nothing turns up. Horrified.

I did not sign up for yet another account at zazzle and checked out as a guest. And now, when I click on the “View Order Details” on their site, it asks me to sign in. They could have told me this before, and I probably would have signed up for an account right? So at this site too I can’t search for or easily find the item I ordered.

The e-mail from CafePress has a UPS link, but no links to their site or a “Order Details” page. Oh man, even here I can’t search for the item I ordered by the name they gave in the email.

Anyway, I’ll say that these 3 sites gave me a sound (not great) experience before checkout (although the post-checkout part leaves a lot to be desired). The extreme example of a poorly-designed site was The American Wedding. Ujwala was looking a site to order some more wedding cards (for friends over here in the US). There were a few sites which were just plain worthless. From their main page you could click on a card type (which will get you to the detail page for that card), but there was no way to start the checkout. No links whatsoever (maybe they do everything over the phone). Maybe they are just ads for a physical business, not e-commerce really.

Anyway, looked like the best bet. We could select an invitation type from a good selection. Then came entering the text. You have to give the bride and the groom names in 3 different places! And the “sub-verse” that they suggested, violated their own rule of max 40 characters on a line for “sub-verses”. But the best – A form element called “Enter the first name of the bride and groom” – which has 3 text-boxes to enter (they are even numbered 1,2 and 3). Ok, they are asking for 2 first names, and clearly are expecting 3 different inputs. Hmmm. Guess what? Turns out Box #2 was for the word “and”! And then they ask “The name of the bride is in Line #” – {select 1 to like 50 from a dropdown}. And the same for the bride. Man!

After filling out all the details, there is no preview of which text goes where in the card. There are no hints as to which section is optional (left top text, right top text) – so if I leave out a section I don’t know if that will result in a blotch of blank in the card. Oh and the shipping was about $20 for standard ground shipping :-) (and I don’t know the post-checkout experience yet).

Now I think I know why Amazon/eBay etc. are where they are in the e-commerce world. Maybe the “invitation cards” industry specifically lacks good sites (which makes me wonder if it is easier to make a difference there), but it seems that most of the “e-commerce” sites are still in the 90s. It seems the internet as a whole has advanced light years ahead of it’s e-commerce children. “web2.0″ is a distant reality for now.

How Ubiquity Saved Me Today

| Comments

I know how I shouldn’t be pointy-clicky on all links I get. Specially the tiny url ones.

Well yesterday I got a link (internal – so can’t share) in a mailing group in office. The URL was a shortened one (something like tinyurl, but used inside Amazon) and it did come with adequate warning about not opening it. But like a few other dumbos I did click it.

I have to share the link in this post – but be warned that I have warned you. It’s no virus r malicious software/code but clever Javascript play. Anyway if you happen to click know that you have no right to sue me whatsoever. And if you really do click – let me know, afterwards, how it goes.

Without further ado, it’s –

I am assuming you know what RickRoll is – if not, you should – the wikipedia article on RickRoll explains it well (it has given me a lot of laughs till date and has robbed some producive hours too. I hope you enjoy it too :-) ).

So, anyway, like a real dumbo, even after reading all the warnings I clicked on it. And bam… my browser is not mine anymore. It’s like one of those really annoying moments when you loose control of something you use all the time (maybe not a great analogy, think a driver loosing control of the steering wheel of his bus. Think a bus full of passengers. In this case, it causes no damage – so like, the bus not hitting any thing/body, but dancing around the street).

I had quite a few interesting tabs open on Firefox. And when it started dancing around, I really lost it. For a few seconds that is. Then the ubiquity idea came to me.

ALT+SHIFT. type “close-tab” (actually just type “clo” and hit tab) and then type “RICK ROLL”. W00t! My browser is back. And my work is saved (the only other way would have been to kill firefox – after which if I restart and ask it to open All Tabs again, I am back to square-one. If I say “Start with no tabs” I loose all the other interesting ones. Of course you can kill FF, open up the FF tabs xml file (it’s nested inside your FF install dir) and delete this tab – but that’s a lot of work).

I have loved Ubiquity a lot and have evangelized it to a lot of people already. It gives you a really powerful browser (like just type “search Something”, or “email Hi to arnab” and you are done. No opening a tabs or another service even). It’s very liberating. My fav commands are search, wiki, weather, email, map, define and twitter.

But yesterday, for the first time I had the opportunity to really feel myself “Thank God I installed Ubiquity”.

If you have not already, give it a spin – here’s the page for the Ubiquity project

If you want more, watch this screencast (you can also find it on the Ubiquity page) –

Ubiquity for Firefox from Aza Raskin on Vimeo.

If you are using it already, don’t you love it? If not using it yet – try it out and let me know how it goes. (Ubiquity is also on twitter – @mozillaubiquity)

Las Vegas Trip Photos

| Comments

And I have to remember this blog for ever. I wrote it right at 1234567890 UTC (and on Friday the 13th!) :-)

$ watch -n1 -d &#039;date +%s; expr 1234567890 - `date +%s`&#039;
Every 1s: date +%s; expr 1234567890 - `date +%s`


Last weekend I, Ujwala and two of my old time friends, Daboo and Nandini met up in Vegas.

We went around, saw the fabulous (should I say decadently smashing) hotels/resorts/casinos and did a little money-wasting (it would be hard to call slot machines gambling). The Cirque Du Soleil Mystere show was really superb – perhaps the best live performance (in theater/circus area) I have seen. The crazy horse show was… well… disappointing.

We had Grand Canyon in mind when we were planning for the trip but heavy snow and bad weather made us cancel it. And Hoover Dam too… :-(

But we did go over to Death Valley National Park Area and Red Rock Canyon – was rocking!

My top five of the trip are – 1) Cirque Du Soleil Mystere show 2) Red Rock Canyon 3) Badwater Basin (and the drive to/from) 4) The fabulous Vegas Strip – special mention goes to Bellagio fountains, the Wynn resort, The Mirage Volcanic fountains 5) The view from top of Stratosphere tower

Taking this opportunity, let me showcase a few of my pictures. All my photos from the trip at arnab-o-scope on smugmug.

Daboo also took some great pictures. They are at Nandini’s picasa album)