Does this seem useful for you?

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

C’est véritablement utile puisque c’est joli.

Antoine de Saint Exupéry

hi robert!

Robert D. [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#M000078

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

2008/9/2 Robert D. [email protected]

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))

On Tue, Sep 2, 2008 at 12:23 PM, Jens W. [email protected]
wrote:

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 )
end

cheers
jens


C’est véritablement utile puisque c’est joli.

Antoine de Saint Exupéry

Robert D. [2008-09-02 12:31]:

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 :wink: i kept hashes, since i sometimes like 'em
better. would you prefer === over is_a? in this case? if so, why?

From: Robert D. [mailto:[email protected]]

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?

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
#=> nil

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

kind regards -botp

On Tue, Sep 2, 2008 at 1:04 PM, Jens W. [email protected]
wrote:

“abcd”.map_messages( [ :[], 1, 2], :size )
done. more or less :wink: 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.

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.

Robert D. [2008-09-02 14:08]:

case args.first
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 :wink: 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