# Duck typing bunches of strings

**URL:** <https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008>\
**Category:** Ruby\
**Created:** [March 27, 2006, 8:39am UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008 "2006-03-27T08:39:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 8:39am UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/1 "2006-03-27T08:39:03Z")

</div>

In my quest to master the fine art of duck typing / overloading, I’ve  
run into this probem:

How can I identify a collection?  
The problem is: String mixes in Enumerable, although it ain’t a  
collection.

My method wants to act differently if it’s given a collection, then if  
it’s just given a single object. I would check .respond\_to? :each -  
but String does, unfortunately.

Currently, I’m forced to do .kind\_of? Array - but this ain’t duck  
typing.

Any ideas? I’d like to separate Array, Hash, Set, etc. etc. from  
single member objects.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/2 "2006-03-27T12:49:24Z")

</div>

What’s wrong with “object.kind\_of?(Enumerable)” and  
“class.include?(Enumerable)”?

my\_array = Array.new  
my\_array.kind\_of?(Enumerable) # =\> true  
Array.include?(Enumerable) # =\> true

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 9:10am UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/3 "2006-03-27T09:10:39Z")

</div>

maybe you just want anything that has respond\_to? :each and isn’t a  
String.

From memory String doing Enumerable is a bit of a special case; if you  
eliminate just String and any of it’s subclasses you’re probably left  
with  
the “real” Enumerables.

;Daniel

On 27/03/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but String does, unfortunately.
> 
> Currently, I’m forced to do .kind\_of? Array - but this ain’t duck  
> typing.
> 
> Any ideas? I’d like to separate Array, Hash, Set, etc. etc. from  
> single member objects.

–  
Daniel B.  
[http://danielbaird.com](http://danielbaird.com) (TiddlyW;nks! :: Whiteboard Koala :: Blog ::  
Things  
That Suck)  
[[My webhost uptime is ~ 92%… if no answer pls call again later!]]

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/4 "2006-03-27T17:44:04Z")

</div>

String is also Enumerable, see OP.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/5 "2006-03-27T15:00:27Z")

</div>

Hi –

On Mon, 27 Mar 2006, removed\_email\_address@domain.invalid wrote:

> Currently, I’m forced to do .kind\_of? Array - but this ain’t duck  
> typing.
> 
> Any ideas? I’d like to separate Array, Hash, Set, etc. etc. from  
> single member objects.

If you really want to do duck typing in your method, then you would  
need to prepare the objects in advance, by having them learn a  
particular method, probably via an extend operation. Then your method  
can just ask each object to execute its version of that method.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/6 "2006-03-27T20:33:18Z")

</div>

On Mar 27, 2006, at 10:43 AM, removed\_email\_address@domain.invalid wrote:

> That wouldn’t be very duckish - I’d still need to explicitly state  
> which objects are acceptable - the whole point is to leave that up to  
> the user, as long as the object can do what it needs to, I never need  
> to hear it’s name.

Just curious, but what happens if you just throw a string at it?  
str.each { |s| } tends to be str most of the time anyway. alternatively:

class Object  
def scalar?  
true # or possibly not respond\_to? :each  
end  
end

module Enumerable  
def scalar?  
false  
end  
end

class String  
def scalar?  
true  
end  
end

Thanks to the magic of inheritance it will all work out

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 28, 2006, 4:54am UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/7 "2006-03-28T04:54:06Z")

</div>

> ri Enumerable  
> The +Enumerable+ mixin provides collection classes with several  
> traversal and searching methods, and with the ability to sort. The  
> class must provide a method +each+, which yields successive members  
> of the collection…

> ri String.each  
> Splits _str_ using the supplied parameter as the record separator  
> (+$/+ by default), passing each substring in turn to the supplied  
> block.

Okay, so maybe String shouldn’t mixin Enumerable in the first place.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 28, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/8 "2006-03-28T17:54:01Z")

</div>

On Tue, 28 Mar 2006 03:33:12 +0900, Logan C. wrote:

> Just curious, but what happens if you just throw a string at it?  
> str.each { |s| } tends to be str most of the time anyway.

From the docs:

Splits _str_ using the supplied parameter as the record separator  
(+$/+ by default), passing each substring in turn to the supplied  
block. If a zero-length record separator is supplied, the string is  
split on +\n+ characters, except that multiple successive newlines  
are appended together.

String#each is actually the same as String#each\_line (one is aliased to  
the other).

To the OP: why do you want to exclude String? Shouldn’t you allow your  
user to consider a string as a collection of lines? That way, someone  
using your library to work on data from a newline-separated file doesn’t  
have to parse it themselves, just load it into a String and call your  
method on it.

Mark T

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/duck-typing-bunches-of-strings/56008/9 "2006-03-27T17:44:36Z")

</div>

That wouldn’t be very duckish - I’d still need to explicitly state  
which objects are acceptable - the whole point is to leave that up to  
the user, as long as the object can do what it needs to, I never need  
to hear it’s name.
