Forum: Ruby the most useful code i've written in a while

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 Ara.T.Howard (Guest)
on 2007-04-05 23:35
(Received via mailing list)
harp:~ > cat errno
#! /usr/bin/env ruby
256.times do |errno|
   puts "#{ errno } => #{ SystemCallError.new(errno).inspect }"
end

harp:~ > errno
0 => #<SystemCallError: Success>
1 => #<Errno::EPERM: Operation not permitted>
2 => #<Errno::ENOENT: No such file or directory>
3 => #<Errno::ESRCH: No such process>
4 => #<Errno::EINTR: Interrupted system call>
5 => #<Errno::EIO: Input/output error>
6 => #<Errno::ENXIO: No such device or address>
7 => #<Errno::E2BIG: Argument list too long>
8 => #<Errno::ENOEXEC: Exec format error>
9 => #<Errno::EBADF: Bad file descriptor>
10 => #<Errno::ECHILD: No child processes>
11 => #<Errno::EAGAIN: Resource temporarily unavailable>
12 => #<Errno::ENOMEM: Cannot allocate memory>
13 => #<Errno::EACCES: Permission denied>
14 => #<Errno::EFAULT: Bad address>
15 => #<Errno::ENOTBLK: Block device required>
16 => #<Errno::EBUSY: Device or resource busy>
17 => #<Errno::EEXIST: File exists>
18 => #<Errno::EXDEV: Invalid cross-device link>
19 => #<Errno::ENODEV: No such device>
20 => #<Errno::ENOTDIR: Not a directory>
21 => #<Errno::EISDIR: Is a directory>
22 => #<Errno::EINVAL: Invalid argument>
23 => #<Errno::ENFILE: Too many open files in system>
24 => #<Errno::EMFILE: Too many open files>
25 => #<Errno::ENOTTY: Inappropriate ioctl for device>
26 => #<Errno::ETXTBSY: Text file busy>
27 => #<Errno::EFBIG: File too large>
28 => #<Errno::ENOSPC: No space left on device>
29 => #<Errno::ESPIPE: Illegal seek>
30 => #<Errno::EROFS: Read-only file system>
31 => #<Errno::EMLINK: Too many links>
32 => #<Errno::EPIPE: Broken pipe>
33 => #<Errno::EDOM: Numerical argument out of domain>
34 => #<Errno::ERANGE: Numerical result out of range>
35 => #<Errno::EDEADLK: Resource deadlock avoided>
36 => #<Errno::ENAMETOOLONG: File name too long>
37 => #<Errno::ENOLCK: No locks available>
38 => #<Errno::ENOSYS: Function not implemented>
39 => #<Errno::ENOTEMPTY: Directory not empty>
40 => #<Errno::ELOOP: Too many levels of symbolic links>
41 => #<SystemCallError: Unknown error 41>
42 => #<Errno::ENOMSG: No message of desired type>
43 => #<Errno::EIDRM: Identifier removed>
44 => #<Errno::ECHRNG: Channel number out of range>
45 => #<Errno::EL2NSYNC: Level 2 not synchronized>
46 => #<Errno::EL3HLT: Level 3 halted>
47 => #<Errno::EL3RST: Level 3 reset>
48 => #<Errno::ELNRNG: Link number out of range>
49 => #<Errno::EUNATCH: Protocol driver not attached>
50 => #<Errno::ENOCSI: No CSI structure available>
51 => #<Errno::EL2HLT: Level 2 halted>
52 => #<Errno::EBADE: Invalid exchange>
53 => #<Errno::EBADR: Invalid request descriptor>
54 => #<Errno::EXFULL: Exchange full>
55 => #<Errno::ENOANO: No anode>
56 => #<Errno::EBADRQC: Invalid request code>
57 => #<Errno::EBADSLT: Invalid slot>
58 => #<SystemCallError: Unknown error 58>
59 => #<Errno::EBFONT: Bad font file format>
60 => #<Errno::ENOSTR: Device not a stream>
61 => #<Errno::ENODATA: No data available>
62 => #<Errno::ETIME: Timer expired>
63 => #<Errno::ENOSR: Out of streams resources>
64 => #<Errno::ENONET: Machine is not on the network>
65 => #<Errno::ENOPKG: Package not installed>
66 => #<Errno::EREMOTE: Object is remote>
67 => #<Errno::ENOLINK: Link has been severed>
68 => #<Errno::EADV: Advertise error>
69 => #<Errno::ESRMNT: Srmount error>
70 => #<Errno::ECOMM: Communication error on send>
71 => #<Errno::EPROTO: Protocol error>
72 => #<Errno::EMULTIHOP: Multihop attempted>
73 => #<Errno::EDOTDOT: RFS specific error>
74 => #<Errno::EBADMSG: Bad message>
75 => #<Errno::EOVERFLOW: Value too large for defined data type>
76 => #<Errno::ENOTUNIQ: Name not unique on network>
77 => #<Errno::EBADFD: File descriptor in bad state>
78 => #<Errno::EREMCHG: Remote address changed>
79 => #<Errno::ELIBACC: Can not access a needed shared library>
80 => #<Errno::ELIBBAD: Accessing a corrupted shared library>
81 => #<Errno::ELIBSCN: .lib section in a.out corrupted>
82 => #<Errno::ELIBMAX: Attempting to link in too many shared libraries>
83 => #<Errno::ELIBEXEC: Cannot exec a shared library directly>
84 => #<Errno::EILSEQ: Invalid or incomplete multibyte or wide
character>
85 => #<Errno::ERESTART: Interrupted system call should be restarted>
86 => #<Errno::ESTRPIPE: Streams pipe error>
87 => #<Errno::EUSERS: Too many users>
88 => #<Errno::ENOTSOCK: Socket operation on non-socket>
89 => #<Errno::EDESTADDRREQ: Destination address required>
90 => #<Errno::EMSGSIZE: Message too long>
91 => #<Errno::EPROTOTYPE: Protocol wrong type for socket>
92 => #<Errno::ENOPROTOOPT: Protocol not available>
93 => #<Errno::EPROTONOSUPPORT: Protocol not supported>
94 => #<Errno::ESOCKTNOSUPPORT: Socket type not supported>
95 => #<Errno::EOPNOTSUPP: Operation not supported>
96 => #<Errno::EPFNOSUPPORT: Protocol family not supported>
97 => #<Errno::EAFNOSUPPORT: Address family not supported by protocol>
98 => #<Errno::EADDRINUSE: Address already in use>
99 => #<Errno::EADDRNOTAVAIL: Cannot assign requested address>
100 => #<Errno::ENETDOWN: Network is down>
101 => #<Errno::ENETUNREACH: Network is unreachable>
102 => #<Errno::ENETRESET: Network dropped connection on reset>
103 => #<Errno::ECONNABORTED: Software caused connection abort>
104 => #<Errno::ECONNRESET: Connection reset by peer>
105 => #<Errno::ENOBUFS: No buffer space available>
106 => #<Errno::EISCONN: Transport endpoint is already connected>
107 => #<Errno::ENOTCONN: Transport endpoint is not connected>
108 => #<Errno::ESHUTDOWN: Cannot send after transport endpoint
shutdown>
109 => #<Errno::ETOOMANYREFS: Too many references: cannot splice>
110 => #<Errno::ETIMEDOUT: Connection timed out>
111 => #<Errno::ECONNREFUSED: Connection refused>
112 => #<Errno::EHOSTDOWN: Host is down>
113 => #<Errno::EHOSTUNREACH: No route to host>
114 => #<Errno::EALREADY: Operation already in progress>
115 => #<Errno::EINPROGRESS: Operation now in progress>
116 => #<Errno::ESTALE: Stale NFS file handle>
117 => #<Errno::EUCLEAN: Structure needs cleaning>
118 => #<Errno::ENOTNAM: Not a XENIX named type file>
119 => #<Errno::ENAVAIL: No XENIX semaphores available>
120 => #<Errno::EISNAM: Is a named type file>
121 => #<Errno::EREMOTEIO: Remote I/O error>
122 => #<Errno::EDQUOT: Disk quota exceeded>
123 => #<SystemCallError: No medium found>
124 => #<SystemCallError: Wrong medium type>
125 => #<SystemCallError: Operation canceled>
126 => #<SystemCallError: Unknown error 126>
....
....
....
....

-a
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-06 01:58
(Received via mailing list)
On Apr 5, 2007, at 4:34 PM, Ara.T.Howard wrote:

> 17 => #<Errno::EEXIST: File exists>

I was just wondering today if I could rescue this error in a cross-
platform safe way.

James Edward Gray II
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-04-06 02:55
(Received via mailing list)
On Apr 5, 5:34 pm, "Ara.T.Howard" <ara.t.how...@noaa.gov> wrote:
> harp:~ > cat errno
> #! /usr/bin/env ruby
> 256.times do |errno|
>    puts "#{ errno } => #{ SystemCallError.new(errno).inspect }"
> end

Nice.

Hmm... How about an inheritance tree too?

T.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-04-06 11:56
(Received via mailing list)
On 06.04.2007 02:54, Trans wrote:
> Hmm... How about an inheritance tree too?
robert@fussel /cygdrive/c/Temp
$ ruby tr.rb > tree.txt

robert@fussel /cygdrive/c/Temp
$ wc -l tree.txt
897 tree.txt

robert@fussel /cygdrive/c/Temp
$ cat tr.rb
require 'pp'
ins = lambda {|h,k| h[k] = Hash.new(&ins)}
tree = Hash.new(&ins)
ObjectSpace.each_object(Class) {|cl| tree[cl.superclass][cl] = tree[cl]}
pp tree

robert@fussel /cygdrive/c/Temp
$ head -20 tree.txt
{Errno::ESPIPE=>{},
  Errno::ENOLINK=>{},
  Errno::EISCONN=>{},
  ThreadError=>{},
  Errno::ENOEXEC=>{},
  Errno::EL3HLT=>{},
  MatchData=>{},
  Errno::ELIBEXEC=>{},
  Errno::EMFILE=>{},
  Errno::ENETUNREACH=>{},
  StandardError=>
   {RangeError=>{FloatDomainError=>{}},
    ThreadError=>{},
    RegexpError=>{},
    NameError=>{NoMethodError=>{}},
    IOError=>{EOFError=>{}},
    RuntimeError=>{},
    SystemStackError=>{},
    TypeError=>{},
    SecurityError=>{},

robert@fussel /cygdrive/c/Temp
$

  robert
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2007-04-06 16:13
(Received via mailing list)
On 4/6/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >
> $ cat tr.rb
> require 'pp'
> ins = lambda {|h,k| h[k] = Hash.new(&ins)}
> tree = Hash.new(&ins)
> ObjectSpace.each_object(Class) {|cl| tree[cl.superclass][cl] = tree[cl]}
> pp tree

Nice! Small refinement for tree of Exceptions:

  tree[cl.superclass][cl] = tree[cl] if cl <= Exception

Regards,
Sean
This topic is locked and can not be replied to.