Forum: Ruby The fun continues

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.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-04 16:16
(Received via mailing list)
class Class
     def to_proc
       proc{new}
     end
   end

   (1..5).map(&Array)  => [[], [], [], [], []]
   (1..5).map(&String) => ["", "", "", "", ""]

Mmmmmmmmm, Ruuuuuuubyyyyyyy... *drool*
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-04 16:52
(Received via mailing list)
Daniel Schierbeck wrote:
>   class Class
>     def to_proc
>       proc{new}
>     end
>   end
>
>   (1..5).map(&Array)  => [[], [], [], [], []]
>   (1..5).map(&String) => ["", "", "", "", ""]

Even cooler:

   class Class
     def to_proc
       proc{|*args| new(*args)}
     end
   end

   class Person
     def initialize(name)
       @name = name
     end

     def inspect
       @name.to_str
     end
   end

   %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]


Daniel
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-06-04 20:29
(Received via mailing list)
Daniel Schierbeck wrote:
> Even cooler:
>     end
>
>     def inspect
>       @name.to_str
>     end
>   end
>
>   %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
>
>
> Daniel

This could have been done just as well by modifying Enumerable#map in
the core, without adding new syntax.

Regards,

Dan
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-06-04 23:22
(Received via mailing list)
On Jun 4, 2006, at 2:27 PM, Daniel Berger wrote:

>> Even cooler:
>>       @name.to_str
> Dan
>

New syntax?
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-05 00:12
(Received via mailing list)
Daniel Berger wrote:
> This could have been done just as well by modifying Enumerable#map in
> the core, without adding new syntax.

Where's the fun in that? :)

I simply had to share my love with the wonderful #to_proc, which
continues to amaze me with its flexibility.


Daniel
This topic is locked and can not be replied to.