# Does this seem useful for you?

**URL:** <https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484>\
**Category:** Ruby\
**Created:** [September 2, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484 "2008-09-02T12:21:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/1 "2008-09-02T12:21:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/2 "2008-09-02T12:30:01Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/3 "2008-09-02T12:31:46Z")

</div>

2008/9/2 Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/4 "2008-09-02T12:38:48Z")

</div>

On Tue, Sep 2, 2008 at 12:23 PM, Jens W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/5 "2008-09-02T13:11:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/6 "2008-09-02T12:46:25Z")

</div>

From: Robert D. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/7 "2008-09-02T14:16:50Z")

</div>

On Tue, Sep 2, 2008 at 1:04 PM, Jens W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/8 "2008-09-02T14:21:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 2, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/does-this-seem-useful-for-you/147484/9 "2008-09-02T14:33:52Z")

</div>

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