From: Meino Christian Cramer Date: 2003-09-13T19:53:18+09:00 Subject: Re: Duck Typing From: Hal Fulton Subject: Re: Duck Typing Date: Sat, 13 Sep 2003 17:13:17 +0900 Hi Hal, thanks for your reply ! > Don't misunderstand. David is saying that this is *some* people's > impression of what duck typing means. > > Note the second part: "--when in fact it's really Ruby that's doing > the duck typing, and you're just deciding how to react to it." > > It's not a coding style or technique; it's an attribute of the > language. Ok...but to use this type of duck inside of ruby...I have code /something/... and if something else seems (at least to me... ;) do the same trick...I am curious why it is better to trigger that duck inside of ruby. Or am I still wrong here... Another code snippet I did in irb gaves me headaches also. Here it is: >> a=Array.new // a shiny new array, full of ducks ;) => [] >> a=[[1,2],3] // fill in some extra ducks => [[1, 2], 3] >> a.respond_to?(:[]) // check whether we have an array => true // expected... >> a[0].respond_to?(:[]) // check wether first entry itself is // an array => true // ok, expected, too >> a[1].respond_to?(:[]) // now the "pure " 3...is it an array, too ? => true // OH! SURPRISE! It is an array...! (?) >> b=a[1] // lets put it in a clean variable => 3 >> b.respond_to?(:[]) // now... => true // Again ... it seems to array-able What do I wrong here??? Background is the following: I have either an hash of the following pattern a=Hash.new a=["", ["","filename]] OR of that pattern: a=["", [["","filename],["","filename],....]] To print both versions, I have handle the value of the hash differently. Currently my method tries to figure out, how to print, with the help of "respond_to?". And it fails due to the above things I did in irb. I am sure, that I have something understood totally wrong here...but... Kind regards, Meino > Hal > > >