[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Does this seem useful for you?

Robert Dober

9/2/2008 10:16:00 AM

Hi list

class Object
def map_messages *messages
messages.map{ | message | send message }
end
end

Does anybody else than me think that this would be nice to have in core?

Cheers
Robert
--=20
C'est v=E9ritablement utile puisque c'est joli.

Antoine de Saint Exup=E9ry

8 Answers

Jens Wille

9/2/2008 10:24:00 AM

0

hi robert!

Robert Dober [2008-09-02 12:15]:
> Does anybody else than me think that this would be nice to have
> in core?
+1 from me. i already have such a thing in my ruby-nuggets library:

<http://prometheus.rubyforge.org/ruby-nuggets/classes/Object.html#M...

argument handling could be improved, i guess, but even a basic
implementation like yours (w/o accepting arguments for individual
messages) would be nice to have.

cheers
jens

James Coglan

9/2/2008 10:25:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

2008/9/2 Robert Dober <robert.dober@gmail.com>

> Hi list
>
> class Object
> def map_messages *messages
> messages.map{ | message | send message }
> end
> end
>
> Does anybody else than me think that this would be nice to have in core?



Interesting, though not sure it would save much effort...

foo.map_messages %w(upcase downcase to_sym)

vs.

%w(upcase downcase to_sym).map(&foo.method(:send))

Robert Dober

9/2/2008 10:32:00 AM

0

On Tue, Sep 2, 2008 at 12:23 PM, Jens Wille <jens.wille@uni-koeln.de> wrote=
:
> hi robert!
>
> Robert Dober [2008-09-02 12:15]:
>> Does anybody else than me think that this would be nice to have
>> in core?
> +1 from me. i already have such a thing in my ruby-nuggets library:
>
> <http://prometheus.rubyforge.org/ruby-nuggets/classes/Object.htm...
>
>
> argument handling could be improved, i guess, but even a basic
> implementation like yours (w/o accepting arguments for individual
> messages) would be nice to have.
Hmm maybe like this

def map_messages( *args )
args.map{ | message_and_args |
m =3D Array =3D=3D=3D message_and_args ? message_and_args : [ messa=
ge_and_args ]
send *m
}
end

"abcd".map_messages( [ :[], 1, 2], :size )
end
>
> cheers
> jens
>
>



--=20
C'est v=E9ritablement utile puisque c'est joli.

Antoine de Saint Exup=E9ry

Peña, Botp

9/2/2008 10:40:00 AM

0

From: Robert Dober [mailto:robert.dober@gmail.com]=20
# class Object
# def map_messages *messages
# messages.map{ | message | send message }
# end
# end
# Does anybody else than me think that this would be nice to=20
# have in core?

it is nice, but i still cannot pass messages w args.

something like eg,

class Object
def map_messages *messages
messages.map{|msg| instance_eval msg.to_s}
end
end
#=3D> nil

"test".map_messages :upcase, "downcase", "slice(1,3)"
#=3D> ["TEST", "test", "est"]

kind regards -botp

Jens Wille

9/2/2008 11:05:00 AM

0

Robert Dober [2008-09-02 12:31]:
> On Tue, Sep 2, 2008 at 12:23 PM, Jens Wille <jens.wille@uni-koeln.de> wrote:
>> +1 from me. i already have such a thing in my ruby-nuggets library:
>>
>> <http://prometheus.rubyforge.org/ruby-nuggets/classes/Object.html#M...
>>
>> argument handling could be improved, i guess, but even a basic
>> implementation like yours (w/o accepting arguments for individual
>> messages) would be nice to have.
> Hmm maybe like this
>
> def map_messages( *args )
> args.map{ | message_and_args |
> m = Array === message_and_args ? message_and_args : [ message_and_args ]
> send *m
> }
> end
>
> "abcd".map_messages( [ :[], 1, 2], :size )
done. more or less ;-) i kept hashes, since i sometimes like 'em
better. would you prefer === over is_a? in this case? if so, why?

Robert Dober

9/2/2008 12:09:00 PM

0

On Tue, Sep 2, 2008 at 1:04 PM, Jens Wille <jens.wille@uni-koeln.de> wrote:

>> "abcd".map_messages( [ :[], 1, 2], :size )
> done. more or less ;-) i kept hashes, since i sometimes like 'em
> better. would you prefer === over is_a? in this case? if so, why?

Although is_a? is more readable the #=== operator is a very powerful
concept of ruby, using it and thus understanding its semantics allows
us to use/understand case statements more efficiently, a problem that
frequently comes up with nubies.
I guess most people prefer this code

case args.first
when String, Symbol
...
when Enumerable
...
end

to

if args.first.is_a?( String ) || ...
...
elsif args.first.is_a?( Enumerable )

end

Somehow I feel therefore the use of Class#=== preferable to
Object#is_a? but I would not argue with people using is_a? ;).

R.

Pedro Silva

9/2/2008 12:16:00 PM

0

Hi,

> def map_messages( *args )
> args.map{ | message_and_args |
> m = Array === message_and_args ? message_and_args : [
> message_and_args ]
> send *m
> }
> end
>
> "abcd".map_messages( [ :[], 1, 2], :size )
> end

At least in Ruby 1.8.6 (the version I've installed) you dont need to
test if it's an Array.

puts *[1] # 1
puts *1 # 1

Supposing that 1.9 maintains that behaviour, I propose the following:

class Object
def map_messages(*args)
args.map{ |msg_and_args| send *msg_and_args }
end

def map_messages_with_block(*args)
args.map do |msg|
if !msg.is_a?(Array) || !msg.last.is_a?(Proc)
send(*msg)
else
send(*msg[0...-1], &msg.last)
end
end
end
end

Obvious map_messages_with_block has a performance penalty, that's why I
kept both.

puts "abcd".map_messages([:[], 1, 2], :upcase, :downcase).inspect

blk = Proc.new{ |obj| obj.map_messages([:[], 1, 2], :upcase, :downcase)}
puts ["abcd", "bcde"].map_messages_with_block([:each, blk], [:[],
1]).inspect

Tell me what you think about it.

Pedro.
--
Posted via http://www.ruby-....

Jens Wille

9/2/2008 12:24:00 PM

0

Robert Dober [2008-09-02 14:08]:
> On Tue, Sep 2, 2008 at 1:04 PM, Jens Wille <jens.wille@uni-koeln.de> wrote:
>> done. more or less ;-) i kept hashes, since i sometimes like 'em
>> better. would you prefer === over is_a? in this case? if so, why?
>
> Although is_a? is more readable the #=== operator is a very powerful
> concept of ruby, using it and thus understanding its semantics allows
> us to use/understand case statements more efficiently, a problem that
> frequently comes up with nubies.
> I guess most people prefer this code
>
> case args.first
> when String, Symbol
> ...
> when Enumerable
> ...
> end
>
> to
>
> if args.first.is_a?( String ) || ...
> ...
> elsif args.first.is_a?( Enumerable )
>
> end
FULL ACK up to this point.

> Somehow I feel therefore the use of Class#=== preferable to
> Object#is_a?
well, i don't ;-) as you said, the latter reads much nicer and is a
straight translation of what i wanted to say. the former just feels
"the wrong way around" to me. and, you see, with the case statement
it's the right way again: you compare an object to a class.

> but I would not argue with people using is_a? ;).
ok, then i won't continue doing so either *g*

cheers
jens