# Functional Equivalency

**URL:** <https://www.ruby-forum.com/t/functional-equivalency/73583>\
**Category:** Ruby\
**Created:** [October 25, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/functional-equivalency/73583 "2006-10-25T14:51:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 25, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/1 "2006-10-25T14:51:23Z")

</div>

This works for multiple keywords:

res = adwords.estimateKeywordList([AdWords::KeywordRequest.new(nil,  
1000000, false, ‘test’, ‘Broad’), AdWords::KeywordRequest.new(nil,  
1000000, false, ‘student’, ‘Broad’)])

This doesn’t:

keywords = [‘test’,‘student’]  
res = adwords.estimateKeywordList([keywords.map! {|x|  
AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)} ])

Aren’t these statements functionaly equivilant, or have I missed  
something (probably!)…this may be an api problem as well.

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 25, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/2 "2006-10-25T14:53:23Z")

</div>

Hi –

On Wed, 25 Oct 2006, tom.mancino wrote:

> AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)} ])
> 
> Aren’t these statements functionaly equivilant, or have I missed  
> something (probably!)…this may be an api problem as well.

map! returns an array – so in the second example, you’ve got an array  
inside an array.

David

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 25, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/3 "2006-10-25T16:05:36Z")

</div>

On 10/25/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > This doesn’t:
> > 
> > keywords = [‘test’,‘student’]  
> > res = adwords.estimateKeywordList([keywords.map! {|x|  
> > AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)} ])
> > 
> > Aren’t these statements functionaly equivilant, or have I missed  
> > something (probably!)…this may be an api problem as well.
> 
> map! returns an array – so in the second example, you’ve got an array  
> inside an array.

I don’t think so. They should be equivalent. Here’s a simplification  
of the OPs use case, with a dummy KeywordRequest class and JUST the  
expression for the argument:

rick@frodo:/public/rubyscripts$ cat kwl.rb  
class KeywordRequest  
def initialize(kwd)  
@kwd = kwd  
end

def inspect  
“KWR(#{@kwd})”  
end

end

puts “With array literal”  
p [KeywordRequest.new(‘test’), KeywordRequest.new(‘student’)]

keywords = [‘test’, ‘student’]  
puts “With map”  
puts “result=#{keywords.map {|x| KeywordRequest.new(x)}.inspect}”  
puts “keywords=#{keywords.inspect}”  
puts “with map!”  
puts “result=#{keywords.map! {|x| KeywordRequest.new(x)}.inspect}”  
puts “keywords=#{keywords.inspect}”

rick@frodo:/public/rubyscripts$ ruby kwl.rb  
With array literal  
[KWR(test), KWR(student)]  
With map  
result=[KWR(test), KWR(student)]  
keywords=[“test”, “student”]  
with map!  
result=[KWR(test), KWR(student)]  
keywords=[KWR(test), KWR(student)]

Both forms return a single level array of KeywordRequests.

It is probably better to use :map instead of :map! unless he WANTS to  
clobber the array of keywords.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 25, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/4 "2006-10-25T19:52:22Z")

</div>

Rick DeNatale wrote:

> > > 1000000, false, ‘student’, ‘Broad’)])  
> > > map! returns an array – so in the second example, you’ve got an array  
> > > @kwd = kwd
> 
> With array literal  
> It is probably better to use :map instead of :map! unless he WANTS to  
> clobber the array of keywords.

The point wasn’t that map and map! are different - it’s that the OP  
takes the result from map! and puts it inside an array (note the square  
brackets):

res = adwords.estimateKeywordList([keywords.map! {|x|  
AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)} ])

Which makes it an array within an array.

With the first form, it’s a flat array:

res = adwords.estimateKeywordList([AdWords::KeywordRequest.new(nil,  
1000000, false, ‘test’, ‘Broad’), AdWords::KeywordRequest.new(nil,  
1000000, false, ‘student’, ‘Broad’)])

The difference is probably why the second form is failing.

-Justin

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 26, 2006, 12:06am UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/5 "2006-10-26T00:06:51Z")

</div>

Justin,

I believe you must be correct, as it does produce different results.  
Any idea on how you would build a flat array that can take a variable  
in this situation?

Rick and David, thanks for any help as well.

Tommy  
Ruby N.

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 26, 2006, 12:10am UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/6 "2006-10-26T00:10:51Z")

</div>

Hi –

On Thu, 26 Oct 2006, tom.mancino wrote:

> > > > > Aren’t these statements functionaly equivilant, or have I missed  
> > > > > class KeywordRequest  
> > > > > puts “With array literal”  
> > > > > rick@frodo:/public/rubyscripts$ ruby kwl.rb
> > 
> > res = adwords.estimateKeywordList([AdWords::KeywordRequest.new(nil, 1000000, false, ‘test’, ‘Broad’), AdWords::KeywordRequest.new(nil, 1000000, false, ‘student’, ‘Broad’)])
> > 
> > The difference is probably why the second form is failing.
> > 
> > -Justin  
> > Justin,
> 
> I believe you must be correct, as it does produce different results.  
> Any idea on how you would build a flat array that can take a variable  
> in this situation?

I’m not sure what you mean by ‘take a variable’, but try just using  
map (or map!) without wrapping it in ann array:

res = adwords.estimateKeywordList(keywords.map! {|x|  
AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)})

(Note lack of square brackets.)

David

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 26, 2006, 1:32am UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/7 "2006-10-26T01:32:59Z")

</div>

On 10/25/06, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > res = adwords.estimateKeywordList([AdWords::KeywordRequest.new(nil,  
> > > > something (probably!)…this may be an api problem as well.  
> > > > class KeywordRequest  
> > > > puts “With array literal”
> > 
> > Both forms return a single level array of KeywordRequests.  
> > res = adwords.estimateKeywordList([keywords.map! {|x| AdWords::KeywordRequest.new(nil, 1000000, false, x, ‘Broad’)}])  
> > Darn, I KNEW it was time to get these glasses checked!

---

<div class="post-metadata">

**Author:** ![tom.mancino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a698b9/32.png) [@tom.mancino](https://www.ruby-forum.com/u/tom.mancino)\
**Post date:** [October 26, 2006, 12:26am UTC](https://www.ruby-forum.com/t/functional-equivalency/73583/8 "2006-10-26T00:26:54Z")

</div>

Worked like a champ, thanks. I really like this language!
