Functional Equivalency

T

tom.mancino

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

dblack

Hi --

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.

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


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
R

Rick DeNatale

Hi --

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.

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

Justin Collins

Rick said:
Hi --

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.

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.

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
 
T

tom.mancino

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 Noob

Rick said:
Hi --
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.
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

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

dblack

Hi --

Rick said:
On 10/25/06, (e-mail address removed) <[email protected]> wrote:
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.
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

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
T

tom.mancino

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

Hi --



Rick DeNatale wrote:
Hi --
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.
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.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 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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1]http://www.manning.com/black| [3]http://www.rubypowerandlight.com
[2]http://dablog.rubypal.com | [4]http://www.rubycentral.org
 
R

Rick DeNatale

Rick said:
Hi --

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

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.

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.

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')} ])

Darn, I KNEW it was time to get these glasses checked! <G>
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top