Forum: Ruby undefined method `map'

Posted by Marcelo Laia (Guest)
on 2012-11-04 22:35
(Received via mailing list)
Hi,

A biostar user helped me with a code to help me download fasta files
from KEGG. The code is named get_fasta4

-----CODE START HERE--------
#!/usr/bin/env ruby
require "rubygems"
require "bio"

serv = Bio::KEGG::API.new

# search for xac + hypothetical
xac = serv.bfind("T00084 hypothetical")
# get the IDS into an array
ids = xac.map { |gene| $1 if gene =~/^(.*?)\s+/ }
# retrieve fasta and print
ids.each { |id| puts serv.bget("-f -n 1 #{id}") }
-----CODE END HERE--------

However, in my system, Debian Testing, I got an error:

marcelo@marcelo:~$ get_fasta4
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': iconv
will be deprecated in the future, use String#encode instead.
/home/marcelo/bin/scripts/get_fasta4:10:in `<main>': undefined method
`map' for #<String:0x87a9f38> (NoMethodError)
marcelo@marcelo:~$
marcelo@marcelo:~$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [i486-linux]
marcelo@marcelo:~$
marcelo@marcelo:~$ gem search soap

*** LOCAL GEMS ***

soap4r (1.5.8)
soap4r-ruby1.9 (2.0.5)
soap4r-ruby19 (1.5.9)
marcelo@marcelo:~$

Could you help me?

Thank you very much!
Posted by Henry Maddocks (Guest)
on 2012-11-04 23:17
(Received via mailing list)
On 5/11/2012, at 10:35 AM, Marcelo Laia <marcelolaia@gmail.com> wrote:

It's telling you what the problem is.

> # search for xac + hypothetical
> xac = serv.bfind("T00084 hypothetical")
> # get the IDS into an array
> ids = xac.map { |gene| $1 if gene =~/^(.*?)\s+/ }
> # retrieve fasta and print
> ids.each { |id| puts serv.bget("-f -n 1 #{id}") }
> -----CODE END HERE--------
>

> /home/marcelo/bin/scripts/get_fasta4:10:in `<main>': undefined method
> `map' for #<String:0x87a9f38> (NoMethodError)


xac is a String which doesn't have a method 'map'. 'map' expects an 
Array. You can either force xac to be an Array

ids = Array(xac).map { |gene| $1 if gene =~/^(.*?)\s+/ }

or fix the previous line to return an array.

Henry
Posted by Ryan Davis (Guest)
on 2012-11-05 00:40
(Received via mailing list)
On Nov 4, 2012, at 14:15 , Henry Maddocks <hmaddocks@me.com> wrote:

>> ids.each { |id| puts serv.bget("-f -n 1 #{id}") }
>> -----CODE END HERE--------
>>
>
>> /home/marcelo/bin/scripts/get_fasta4:10:in `<main>': undefined method
>> `map' for #<String:0x87a9f38> (NoMethodError)
>
>
> xac is a String which doesn't have a method 'map'. 'map' expects an Array.

#map is a member of Enumerable, not just Array. String used to be 
Enumerable in 1.8-, but it isn't in 1.9+

10000 % irb
>> String.ancestors
=> [String, Enumerable, Comparable, Object, Kernel]
10001 % irb19
>> String.ancestors
=> [String, Comparable, Object, Kernel, BasicObject]

> You can either force xac to be an Array
>
> ids = Array(xac).map { |gene| $1 if gene =~/^(.*?)\s+/ }

xac.lines.map is clearer.

There should probably also be a #compact since the regexp could 
potentially not match.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.