Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A simple newbie question (arrays and strings)

Reply
Thread Tools

A simple newbie question (arrays and strings)

 
 
koichirose
Guest
Posts: n/a
 
      05-27-2008
koichirose wrote:
> Yes, .downcase, confirmed


I have a problem, why does this work:
list = Dir.entries('.')[2..-1].map{|f| f.split(' - ')[0].capitalize}

And this doesn't?
list2 = Dir.entries('.')[2..-1].map{|f| f.split(' - ')[1].capitalize}

It returns: hello.rb:7: undefined method `capitalize' for nil:NilClass
(NoMethodError)
from hello.rb:7:in `map'

Thank you
 
Reply With Quote
 
 
 
 
Dave Bass
Guest
Posts: n/a
 
      05-27-2008
koichirose wrote:
> It returns: hello.rb:7: undefined method `capitalize' for nil:NilClass
> (NoMethodError)


That error message means you're trying to call the capitalize method on
something that doesn't exist. I.e. xxx.capitalise where xxx evaluates to
nil. The class NilClass doesn't have a capitalize method. Your problem
is with xxx; trying "p"-ing it.





--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
koichirose
Guest
Posts: n/a
 
      05-27-2008
Dave Bass wrote:
> That error message means you're trying to call the capitalize method on
> something that doesn't exist. I.e. xxx.capitalise where xxx evaluates to
> nil. The class NilClass doesn't have a capitalize method. Your problem
> is with xxx; trying "p"-ing it.
>



Ok, but those 2 lines are the same, except that with list2 I take the
part after ' - '.
If I do:

#~ j=0
#~ for i in list2
#~ puts list2[j]
#~ j +=1
#~ end

it works. Why are list and list2 behaving differently?
 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      05-27-2008
On Tue, May 27, 2008 at 12:14 PM, koichirose <(E-Mail Removed)> wrote:
> Dave Bass wrote:
> Ok, but those 2 lines are the same, except that with list2 I take the part
> after ' - '.
> If I do:
>
> #~ j=0
> #~ for i in list2
> #~ puts list2[j]
> #~ j +=1
> #~ end


You have entries that either have no dash "-", or have one but nothing
after. You can NilClass#puts, but not just any method on it, such as
#capitalize. If you don't need them (like, for example, counting
purposes), remove your nils out of the array list with #compact.
Example in irb...

[1, nil, 2].compact
=> [1, 2]

Todd

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
Hey common folks let a "newbie" in and please answer my question! :) (even if its darn simple!) Robocop Java 2 02-22-2004 04:14 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments