Pretty Print error with File.stat

D

Daniel Berger

Hi all,

Ruby 1.8.2
Windows XP Pro

This should work out of the box, shouldn't it?

irb(main):001:0> File.stat("test.rb")
=> #<File::Stat dev=0x2, ino=0, mode=0100644, nlink=1, uid=0, gid=0,
rdev=0x2, size=719, blksize=nil, blocks=nil, atime=Fri Feb 04 19:33:07
Mountain Standard Time 2005, mtime=Fri Feb 04 19:21:03 Mountain
Standard Time 2005, ctime=Fri Feb 0419:33:07 Mountain Standard Time
2005>
irb(main):002:0> require "pp"
=> true
irb(main):003:0> pp File.stat("test.rb")
#<File::StatNoMethodError: undefined method `name' for nil:NilClass
from c:/ruby/lib/ruby/1.8/pp.rb:410:in `pretty_print'
from c:/ruby/lib/ruby/1.8/pp.rb:407:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `nest'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group_sub'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group'
from c:/ruby/lib/ruby/1.8/pp.rb:407:in `pretty_print'
from c:/ruby/lib/ruby/1.8/pp.rb:382:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `nest'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group_sub'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group'
from c:/ruby/lib/ruby/1.8/pp.rb:218:in `object_group'
from c:/ruby/lib/ruby/1.8/pp.rb:382:in `pretty_print'
from c:/ruby/lib/ruby/1.8/pp.rb:211:in `pp'
from c:/ruby/lib/ruby/1.8/pp.rb:211:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `nest'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:223:in `group'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group_sub'
from c:/ruby/lib/ruby/1.8/prettyprint.rb:222:in `group'
from c:/ruby/lib/ruby/1.8/pp.rb:211:in `pp'
from c:/ruby/lib/ruby/1.8/pp.rb:165:in `pp'
from c:/ruby/lib/ruby/1.8/pp.rb:165:in `guard_inspect_key'
from c:/ruby/lib/ruby/1.8/pp.rb:165:in `pp'
from c:/ruby/lib/ruby/1.8/pp.rb:155:in `pp'
from c:/ruby/lib/ruby/1.8/pp.rb:154:in `each'
from c:/ruby/lib/ruby/1.8/pp.rb:154:in `pp'

This isn't an irb issue, because it does the same thing if run as a
standalone script.

I thought "pp" worked for just about everything. Why is this failing?

Regards,

Dan
 
T

ts

D> irb(main):001:0> File.stat("test.rb")
D> => #<File::Stat dev=0x2, ino=0, mode=0100644, nlink=1, uid=0, gid=0,
D> rdev=0x2, size=719, blksize=nil, blocks=nil, atime=Fri Feb 04 19:33:07
D> Mountain Standard Time 2005, mtime=Fri Feb 04 19:21:03 Mountain
D> Standard Time 2005, ctime=Fri Feb 0419:33:07 Mountain Standard Time
2005>

Can you try

ruby -retc -e 'p Etc.getpwuid(0)'

D> #<File::StatNoMethodError: undefined method `name' for nil:NilClass

if it return `nil' this can explain the error

uln% ruby -e 'nil.name'
-e:1: undefined method `name' for nil:NilClass (NoMethodError)
uln%


Guy Decoux
 
T

Tanaka Akira

Daniel Berger said:
Ruby 1.8.2
Windows XP Pro
irb(main):003:0> pp File.stat("test.rb")
#<File::StatNoMethodError: undefined method `name' for nil:NilClass
from c:/ruby/lib/ruby/1.8/pp.rb:410:in `pretty_print'

OS dependent?

Does following patch fix it?

Index: lib/pp.rb
===================================================================
RCS file: /src/ruby/lib/pp.rb,v
retrieving revision 1.38
diff -u -p -r1.38 pp.rb
--- lib/pp.rb 14 Nov 2004 04:27:12 -0000 1.38
+++ lib/pp.rb 6 Feb 2005 01:28:00 -0000
@@ -407,18 +407,24 @@ class File
q.group {
q.text "uid="; q.pp self.uid
begin
- name = Etc.getpwuid(self.uid).name
- q.breakable; q.text "(#{name})"
+ pw = Etc.getpwuid(self.uid)
rescue ArgumentError
end
+ if pw
+ name = pw.name
+ q.breakable; q.text "(#{name})"
+ end
}
q.comma_breakable
q.group {
q.text "gid="; q.pp self.gid
begin
- name = Etc.getgrgid(self.gid).name
- q.breakable; q.text "(#{name})"
+ gr = Etc.getgrgid(self.gid)
rescue ArgumentError
+ end
+ if gr
+ name = gr.name
+ q.breakable; q.text "(#{name})"
end
}
q.comma_breakable
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top