Forum: Ruby Duck typing bunches of strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2006-03-27 08:39
(Received via mailing list)
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.
Bd0203dc8478deb969d72f52e741bd4f?d=identicon&s=25 Daniel Baird (Guest)
on 2006-03-27 09:10
(Received via mailing list)
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, listrecv@gmail.com <listrecv@gmail.com> 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 Baird
http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things
That Suck)
[[My webhost uptime is ~ 92%.. if no answer pls call again later!]]
1ca3a0180e953e56d528a3b5ddb3d6b3?d=identicon&s=25 Jason Hutchens (Guest)
on 2006-03-27 12:49
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-27 15:00
(Received via mailing list)
Hi --

On Mon, 27 Mar 2006, listrecv@gmail.com 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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2006-03-27 17:44
(Received via mailing list)
String is also Enumerable, see OP.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2006-03-27 17:44
(Received via mailing list)
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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-27 20:33
(Received via mailing list)
On Mar 27, 2006, at 10:43 AM, listrecv@gmail.com 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
1ca3a0180e953e56d528a3b5ddb3d6b3?d=identicon&s=25 Jason Hutchens (Guest)
on 2006-03-28 04:54
(Received via mailing list)
> 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.
852c7ee4bd3e67f39b49d08f2c4ea6aa?d=identicon&s=25 Mark T (Guest)
on 2006-03-28 17:54
(Received via mailing list)
On Tue, 28 Mar 2006 03:33:12 +0900, Logan Capaldo 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
This topic is locked and can not be replied to.