From: James O'Brien on
[Note: parts of this message were removed to make it a legal post.]

File.open('myfile') do |f|
puts f.basename;
end

myfile exists on the filesystem but this code blows up with

undefined method `basename'

could someone explain why (given the docs
http://ruby-doc.org/core/classes/File.html
advertise the basename method)

[ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]


Thanks!

From: MrZombie on
You do have FileUtils installed and required, yes?

require 'ftools'

On 2010-07-20 10:08:40 -0400, James O'Brien said:

> [Note: parts of this message were removed to make it a legal post.]
>
> File.open('myfile') do |f|
> puts f.basename;
> end
>
> myfile exists on the filesystem but this code blows up with
>
> undefined method `basename'
>
> could someone explain why (given the docs
> http://ruby-doc.org/core/classes/File.html
> advertise the basename method)
>
> [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
>
>
> Thanks!


--
Thank you for your brain.
-MrZombie

From: Yaser Sulaiman on
[Note: parts of this message were removed to make it a legal post.]

On Tue, Jul 20, 2010 at 5:08 PM, James O'Brien <jeob32(a)gmail.com> wrote:

> File.open('myfile') do |f|
> puts f.basename;
> end
>
> myfile exists on the filesystem but this code blows up with
>
> undefined method `basename'
>
> could someone explain why (given the docs
> http://ruby-doc.org/core/classes/File.html
> advertise the basename method)
>

Because it's a class method, not an instance method. That is, you should use
it as follows:

File.basename("foo.bar")

HTH,
Yaser

From: Rob Biedenharn on
On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:

> File.open('myfile') do |f|
> puts f.basename;
> end
>
> myfile exists on the filesystem but this code blows up with
>
> undefined method `basename'
>
> could someone explain why (given the docs
> http://ruby-doc.org/core/classes/File.html
> advertise the basename method)
>
> [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
>
>
> Thanks!


You're looking at the docs for the class method File.basename, but
you're calling basename on an instance of File referenced by f

puts File.basename('myfile')

or better:

puts File.basename('/some/long/path/to/myfile')

-Rob

Rob Biedenharn
Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab(a)GaslightSoftware.com http://GaslightSoftware.com/


From: Rob Biedenharn on
On Jul 20, 2010, at 10:52 AM, Rob Biedenharn wrote:
> On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:
>
>> File.open('myfile') do |f|
>> puts f.basename;
>> end
>>
>> myfile exists on the filesystem but this code blows up with
>>
>> undefined method `basename'
>>
>> could someone explain why (given the docs
>> http://ruby-doc.org/core/classes/File.html
>> advertise the basename method)
>>
>> [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
>>
>>
>> Thanks!
>
> You're looking at the docs for the class method File.basename, but
> you're calling basename on an instance of File referenced by f
>
> puts File.basename('myfile')
>
> or better:
>
> puts File.basename('/some/long/path/to/myfile')

Actually, a better example (and likely closer to what you expected):

puts File.basename(f.path)

>
> -Rob
>
> Rob Biedenharn
> Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/
> rab(a)GaslightSoftware.com http://GaslightSoftware.com/
>
>


 |  Next  |  Last
Pages: 1 2
Prev: Ruby Shoes tray icon?
Next: unable to locate component