Does this seem useful for you?

R

Robert Dober

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
 
J

James Coglan

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

2008/9/2 Robert Dober said:
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))
 
R

Robert Dober

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#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.
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
 
P

Peña, Botp

From: Robert Dober [mailto:[email protected]]=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
 
J

Jens Wille

Robert Dober [2008-09-02 12:31]:
+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.
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?
 
R

Robert Dober

"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.
 
P

Pedro Silva

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.
 
J

Jens Wille

Robert Dober [2008-09-02 14:08]:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top