About to_path and to_open

e$B1sF#$H?=$7$^$9!#e(B

e$B$/[email protected]$i$J$$$3$H$G62=L$G$9$,!"e(Brequire 0
e$B$NNc30%a%C%;!<%8$,JQ$G$9!#e(B

$ ./ruby -ve ‘require 0’
ruby 1.9.0 (2007-12-06 patchlevel 0) [i686-linux]
-e:1:in require': can't convert nil into String (TypeError) from -e:1:in

e$B$^$?!"e(BKernel.open e$B$,e(B to_path
e$B$r8F$s$G$/$l$J$$$N$O;EMM$G$7$g$&$+!#e(B

require “pathname”
open(Pathname.new(“foo”)) {|fh| fh.read }
#=> undefined method `to_open’ for #Pathname:foo

Kernel.open e$B$O0z?t$Ne(B to_open
e$B$r8F$V$h$&$G$9$,!"$3$l$O2?$N$?$a$KI,MWe(B
e$B$J$s$G$7$g$&$+!#e(Btrunk e$B$N$J$+$Ge(B to_open
e$B$rDj5A$9$k%/%i%9$O8+$"$?$je(B
e$B$^$;$s$G$7$?!#<+J,$GDj5A$7$?%/%i%9$Ge(B to_open
e$B$rDj5A$9$k$H2?$+4r$7$$e(B
e$B$3$H$,$"$k$N$G$7$g$&$+!#e(B

to_path e$B$J$I$,F3F~$5$l$?7P0^$rCN$i$J$$$N$G!"$H$s$A$s$+$s$J$3$H$re(B
[email protected]$C$F$$$?$i$9$_$^$;$s!#e(B

e$B$b$C$H:3:Y$J$3$H$G$9$,!“e(Bto_path e$B$rDj5A$7$?e(B String e$B$re(B
require e$B$9$k$He(B
String e$B<+?H$,M%@h$5$l$k$h$&$G$9!#$3$l$O;EMM$G$7$g$&$+!#$o$6$o$6e(B
to_path
[email protected]$+$i!”$=$A$i$r8+$F$"$2$kJ}$,5$$,Mx$$$F$$$ke(B
e$B$h$&$J5$$,$7$^$9e(B (e$BK\Ev$K:3:Y$G$4$a$s$J$5$$e(B) e$B!#e(B

file = “e$B!{!_%i%$%V%i%je(B”
def file.to_path; “foobar.rb”; end

puts “#{ file }e$B$r%m!<%I$7$^$9e(B…”
require file #=> no such file to load – e$B!{!_%i%$%V%i%je(B

require 0 e$B$N%(%i!<%a%C%;!<%8$N%Q%C%[email protected]$1Aw$j$^$9!#e(B

Index: file.c

— file.c (revision 14122)
+++ file.c (working copy)
@@ -111,6 +111,7 @@
if (rb_respond_to(obj, to_path)) {
tmp = rb_funcall(obj, to_path, 0, 0);
}

  • if (NIL_P(tmp)) tmp = obj;
    exit:
    StringValueCStr(tmp);
    if (obj != tmp) {

[email protected]$G$9!#e(B

At Thu, 6 Dec 2007 21:34:08 +0900,
Yusuke ENDOH wrote in [ruby-dev:32473]:

e$B$^$?!"e(BKernel.open e$B$,e(B to_path e$B$r8F$s$G$/$l$J$$$N$O;EMM$G$7$g$&$+!#e(B

require “pathname”
open(Pathname.new(“foo”)) {|fh| fh.read }
#=> undefined method `to_open’ for #Pathname:foo

Kernel.open e$B$O0z?t$Ne(B to_open e$B$r8F$V$h$&$G$9$,!"$3$l$O2?$N$?$a$KI,MWe(B
e$B$J$s$G$7$g$&$+!#e(Btrunk e$B$N$J$+$Ge(B to_open e$B$rDj5A$9$k%/%i%9$O8+$"$?$je(B
e$B$^$;$s$G$7$?!#<+J,$GDj5A$7$?%/%i%9$Ge(B to_open e$B$rDj5A$9$k$H2?$+4r$7$$e(B
e$B$3$H$,$"$k$N$G$7$g$&$+!#e(B

to_opene$B$O:G6aF~$C$?$b$N$J$N$G!"e(Bpathname.rbe$B$,L$BP1~$G$9$M!#e(B

Sat Dec 1 10:45:56 2007 Yukihiro M. [email protected]

  • io.c (rb_f_open): use to_open for every non-string object. path
    object may use method_missing.

e$B$b$C$H:3:Y$J$3$H$G$9$,!“e(Bto_path e$B$rDj5A$7$?e(B String e$B$re(B require e$B$9$k$He(B
String e$B<+?H$,M%@h$5$l$k$h$&$G$9!#$3$l$O;EMM$G$7$g$&$+!#$o$6$o$6e(B
to_path [email protected]$+$i!”$=$A$i$r8+$F$"$2$kJ}$,5$$,Mx$$$F$$$ke(B
e$B$h$&$J5$$,$7$^$9e(B (e$BK\Ev$K:3:Y$G$4$a$s$J$5$$e(B) e$B!#e(B

to_pathe$B$Oe([email protected]$Ne(Bto_stre$B$NFCJLHG$H$$$&[email protected]$H;W$$$^$9!#$b$H$b$He(B
Stringe$B$J$ie(Bto_stre$B$O8F$P$l$^$;$s$+$i!#e(Bto_opene$B$J$i$P!“e(Bopene$B$+$i$O8Fe(B
e$B$P$l$k$b$N$Ne(Brequiree$B$+$i$O8F$P$l$^$;$s$,!”$3$l$,K>$^$7$$F0:n$+$Ie(B
e$B$&$+$O$h$/$o$+$j$^$;$s!#e(B

require 0 e$B$N%(%i!<%a%C%;!<%8$N%Q%C%[email protected]$1Aw$j$^$9!#e(B

[email protected]$He(Bto_pathe$B$,e(Bnile$B$rJV$7$?$H$-$KL5;k$5$l$k$3$H$K$J$j$^$;$s$+!#e(B

Index: file.c

— file.c (revision 14122)
+++ file.c (working copy)
@@ -112,4 +112,7 @@ rb_get_path(VALUE obj)
tmp = rb_funcall(obj, to_path, 0, 0);
}

  • else {

  • tmp = obj;

  • }
    exit:
    StringValueCStr(tmp);
    Index: lib/pathname.rb
    ===================================================================
    — lib/pathname.rb (revision 14122)
    +++ lib/pathname.rb (working copy)
    @@ -799,4 +799,6 @@ class Pathname # * File *
    end

  • alias to_open open

  • See File.readlink. Read symbolic link.

    def readlink() self.class.new(File.readlink(@path)) end

e$B1sF#$G$9!#e(B

07/12/07 e$B$Ke(B Nobuyoshi N.[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

to_opene$B$O:G6aF~$C$?$b$N$J$N$G!"e(Bpathname.rbe$B$,L$BP1~$G$9$M!#e(B

e$B$"$l!"$G$be(B to_open e$B$G8!:w$9$k$He(B [ruby-dev:23479]
e$B$,$R$C$+$+$j$^$9!#e(B
e$B$=$N$/$i$$$N$3$m$+$i$O$"$k$s$8$c$J$$$G$7$g$&$+!#L>A0$,F1$8JLJ*!)e(B

Pathname e$B$b4^$a$FB?$/$N>l9g$Oe(B to_path
e$B$G==J,$=$&$J$N$G!"e(Bto_open e$B$Ne(B
e$B4r$7$5$,$d$C$Q$j$h$/$o$+$i$J$$$N$G$9$,!“e(Buri e$B$H$+e(B open-uri
e$B$”$?$j$Ge(B
e$B;H$o$l$kM=Dj$H$+$G$7$g$&$+!#e(B

virtual_filename = “http://example.com/
def virtual_filename.to_open
StringIO.new(“foo bar baz”)
end
open(virtual_filename) {|fh| puts fh.read }

e$B$b$C$H:3:Y$J$3$H$G$9$,!“e(Bto_path e$B$rDj5A$7$?e(B String e$B$re(B require e$B$9$k$He(B
String e$B<+?H$,M%@h$5$l$k$h$&$G$9!#$3$l$O;EMM$G$7$g$&$+!#$o$6$o$6e(B
to_path [email protected]$+$i!”$=$A$i$r8+$F$"$2$kJ}$,5$$,Mx$$$F$$$ke(B
e$B$h$&$J5$$,$7$^$9e(B (e$BK\Ev$K:3:Y$G$4$a$s$J$5$$e(B) e$B!#e(B

to_pathe$B$Oe([email protected]$Ne(Bto_stre$B$NFCJLHG$H$$$&[email protected]$H;W$$$^$9!#$b$H$b$He(B
Stringe$B$J$ie(Bto_stre$B$O8F$P$l$^$;$s$+$i!#e(B

e$B$J$k$[$I!"$=$&$$$&%9%?%s%9$J$i8F$P$J$$$N$,E,@Z$G$9$M!#e(B
e$BG<[email protected]$7$^$7$?!#$"$j$,$H$&$4$6$$$^$9!#e(B

require 0 e$B$N%(%i!<%a%C%;!<%8$N%Q%C%[email protected]$1Aw$j$^$9!#e(B

[email protected]$He(Bto_pathe$B$,e(Bnile$B$rJV$7$?$H$-$KL5;k$5$l$k$3$H$K$J$j$^$;$s$+!#e(B

e$B$=$NDL$j$G$9!#$4$a$s$J$5$$!#e(B

In article [email protected],
Nobuyoshi N. [email protected] writes:

def readlink() self.class.new(File.readlink(@path)) end
e$B8=>u!"$3$l$,$J$$$He(B open e$B$Ke(B Pathname
e$B$,;H$($J$$$N$G%3%_%C%H$7e(B
e$B$F$/[email protected]$5$$!#e(B
(test-all e$B$,@.8y$7$F$$$k$N$Oe(B open-uri e$B$,e(B require
e$B$5$l$F$k$+e(B
e$B$i$NLOMMe(B)

e$B7P0^$H$7$F$O!"$^$:e(B open-uri e$B$N;EMM$H$7$Fe(B (public e$B$Je(B)
open e$B%ae(B
e$B%=%C%I$NOC$,$"$j!"$=$l$+$ie(B to_path e$B$de(B to_open
e$B$,$"$j$^$9!#e(B

e$B;d$NM}2r$9$k$H$3$m$G$Oe(B (e$B8=>u$,0J2<$N$h$&$K$J$C$F$$$k$H$O8B$je(B
e$B$^$;$s$,e(B) e$B<!$N$h$&$J0c$$$,$"$j$^$9!#e(B

open e$B$r<BAu$9$k$H$-$O%V%m%C%/$N=hM}$b$d$kI,MW$,$"$j$^$9!#e(B
e$B$^$?e(B open-uri e$B$re(B require e$B$9$kI,MW$,$"$j$^$9!#e(B

to_open e$B$r<BAu$9$k$H$-$O%V%m%C%/$N=hM}$OITMW$G$9!#e(B
e$B$,!"8e;OKv$N%?%$%_%s%0$,$$$/$i$+LLE]$G$9!#e(B

to_path e$B$r<BAu$9$k$J$i%U%!%$%k$r%!<%W%s$9$k=hM}$bITMW$G$9!#e(B
e$B$,!"%U%!%$%k%7%9%F%`>e$NK\J
$N%U%!%$%k$7$+07$($^$;$s!#e(B

[email protected]$G$9!#e(B

At Fri, 7 Dec 2007 12:49:34 +0900,
Yusuke ENDOH wrote in [ruby-dev:32477]:

to_opene$B$O:G6aF~$C$?$b$N$J$N$G!"e(Bpathname.rbe$B$,L$BP1~$G$9$M!#e(B

e$B$"$l!"$G$be(B to_open e$B$G8!:w$9$k$He(B [ruby-dev:23479] e$B$,$R$C$+$+$j$^$9!#e(B
e$B$=$N$/$i$$$N$3$m$+$i$O$"$k$s$8$c$J$$$G$7$g$&$+!#L>A0$,F1$8JLJ*!)e(B

e$B$$$d!"$9$$$^$;$s!#4*0c$$$G$7$?!#$7$P$i$/A0$Ke(Bruby-coree$B$N$[$&$Ge(B
to_*e$B$H$$$&L>[email protected]$,$"$C$?$H$-$K!“e(Bto_pathe$B$H$+e(Bto_opene$B$b=Pe(B
e$B$F$?3P$($,$”$C$F4V0c$($^$7$?!#e(B

Pathname e$B$b4^$a$FB?$/$N>l9g$Oe(B to_path e$B$G==J,$=$&$J$N$G!"e(Bto_open e$B$Ne(B
e$B4r$7$5$,$d$C$Q$j$h$/$o$+$i$J$$$N$G$9$,!“e(Buri e$B$H$+e(B open-uri e$B$”$?$j$Ge(B
e$B;H$o$l$kM=Dj$H$+$G$7$g$&$+!#e(B

virtual_filename = “http://example.com/
def virtual_filename.to_open
StringIO.new(“foo bar baz”)
end
open(virtual_filename) {|fh| puts fh.read }

e$B$I$&$J$s$G$7$g$&$M!#e(Bto_pathe$B$be(Bto_opene$B$b;H$C$?$3$H$,$J$$$N$G$"$^$je(B
e$B6qBNNc$O;W$$IU$+$J$$$N$G$9$,!#e(B

[email protected]$G$9!#e(B

At Thu, 6 Dec 2007 21:34:08 +0900,
Yusuke ENDOH wrote in [ruby-dev:32473]:

e$B$^$?!"e(BKernel.open e$B$,e(B to_path e$B$r8F$s$G$/$l$J$$$N$O;EMM$G$7$g$&$+!#e(B

require “pathname”
open(Pathname.new(“foo”)) {|fh| fh.read }
#=> undefined method `to_open’ for #Pathname:foo

Stringe$B0J30$G$O>o$Ke(Bto_opene$B$r8F$V$h$&$K$J$C$F$$$^$9$,!“e(BStringe$B$KBPe(B
e$B$7$F$Oe(Bto_pathe$B$O8F$P$l$k$3$H$O$”$j$^$;$s!#e(B

At Fri, 7 Dec 2007 10:02:55 +0900,
Nobuyoshi N. wrote in [ruby-dev:32474]:

to_pathe$B$Oe([email protected]$Ne(Bto_stre$B$NFCJLHG$H$$$&[email protected]$H;W$$$^$9!#e(B

e$B$H$$$&$3$H$G$h$1$l$P!"e(Brb_f_open()e$B$Oe(Bto_stre$B$G$O$J$/$Fe(Bto_pathe$B$r;H$&e(B
e$B$[$&$,$$$$$H;W$$$^$9!#e(B

Index: file.c

— file.c (revision 14122)
+++ file.c (working copy)
@@ -112,4 +112,7 @@ rb_get_path(VALUE obj)
tmp = rb_funcall(obj, to_path, 0, 0);
}

  • else {
  • tmp = obj;
  • }
    exit:
    StringValueCStr(tmp);
    Index: io.c
    ===================================================================
    — io.c (revision 14122)
    +++ io.c (working copy)
    @@ -3800,5 +3800,6 @@ rb_f_open(int argc, VALUE *argv)
    }
    else {
  •  VALUE tmp = rb_check_string_type(argv[0]);
    
  •  VALUE tmp = argv[0];
    
  •  FilePathValue(tmp);
     if (NIL_P(tmp)) {
    
    redirect = Qtrue;

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32480] Re: about to_path and to_open”
on Fri, 7 Dec 2007 13:54:50 +0900, Nobuyoshi N.
[email protected] writes:

|At Fri, 7 Dec 2007 10:02:55 +0900,
|Nobuyoshi N. wrote in [ruby-dev:32474]:
|> to_pathe$B$Oe([email protected]$Ne(Bto_stre$B$NFCJLHG$H$$$&[email protected]$H;W$$$^$9!#e(B
|
|e$B$H$$$&$3$H$G$h$1$l$P!"e(Brb_f_open()e$B$Oe(Bto_stre$B$G$O$J$/$Fe(Bto_pathe$B$r;H$&e(B
|e$B$[$&$,$$$$$H;W$$$^$9!#e(B

e$B%3%_%C%H$7$F$/[email protected]$5$$!#e(B

e$B1sF#$G$9!#e(B

07/12/07 e$B$Ke(B Tanaka A.[email protected] e$B$5$s$O=q$-$^$7$?e(B:

e$B7P0^$H$7$F$O!"$^$:e(B open-uri e$B$N;EMM$H$7$Fe(B (public e$B$Je(B) open e$B%ae(B
e$B%=%C%I$NOC$,$"$j!"$=$l$+$ie(B to_path e$B$de(B to_open e$B$,$"$j$^$9!#e(B

e$B;d$NM}2r$9$k$H$3$m$G$Oe(B (e$B8=>u$,0J2<$N$h$&$K$J$C$F$$$k$H$O8B$je(B
e$B$^$;$s$,e(B) e$B<!$N$h$&$J0c$$$,$"$j$^$9!#e(B

[email protected]@$"$j$,$H$&$4$6$$$^$7$?!#e(B

e$B4*0c$$$7$F$?$i$9$_$^$;$s$,!"%V%m%C%/$N=hM}$H$$$&$N$Oe(B
if block_given?
begin
return yield(fd)
ensure
fd.close
end
else
return fd
end
e$B$H$$$&$/[email protected]$H;W$&$N$G$9$,!"e(Bto_open
e$B$rF3F~$^$G$7$F$3$N=hM}$re(B
Kernel.open e$B$K$d$i$;$?$$M}M3$,$h$/$o$+$j$^$;$s$G$7$?!#e(B

e$B$^$?!“e(Bto_open e$B$N3hMQK!$O!”$A$g$C$H9M$([email protected][email protected]$He(B Uri#to_open
e$B$He(B
e$B%H%j%C%-!<$JM7$S$/$i$$$7$+;W$$$D$+$J$$$s$G$9$,!“B>$K$b2?$+1~MQ$,e(B
e$B$”$k$s$G$7$g$&$+!#e(B

e$B$"$H!"8=:_e(B Uri#to_open e$B$,<BAu$5$l$F$$$J$$e(B (e$B$G$9$h$M!)e(B)
e$B$N$O!“e(B
e$B2?$+M}M3$,$”$k$s$G$7$g$&$+!#C1$K<j$,[email protected]$1!)e(B

e$B1sF#$G$9!#e(B

07/12/07 e$B$Ke(B Yukihiro M.[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

|e$B$^$?!“e(Bto_open e$B$N3hMQK!$O!”$A$g$C$H9M$([email protected][email protected]$He(B Uri#to_open e$B$He(B
|e$B%H%j%C%-!<$JM7$S$/$i$$$7$+;W$$$D$+$J$$$s$G$9$,!“B>$K$b2?$+1~MQ$,e(B
|e$B$”$k$s$G$7$g$&$+!#e(B

e$B!VD>@%U%!%$%k$K7k$S$D$$$F$$$J$$$J$K$+$re(B(Stringe$B$G$J$$%%V%8%'e(B
e$B%/%H$K$h$C$F;XDj$7$Fe(B)e$B%
!<%W%s$7$?$$;~!W$J$s$G$9$,!"7k6I$O<gMWe(B
e$B$JMQES$Oe(BURI e$B%/%i%9$K8BDj$5$l$A$c$&$+$b$7$l$^$;$s$M!#e(B

e$B$&!<$s!"$d$C$Q$j$=$&$G$9$+!#$"$j$,$H$&$4$6$$$^$9!#e(B
e$B2?$+B>$KJXMx$J;H$$J}$,$"$k$h$&$J5$$b$9$k$s$G$9$1$I$M!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32481] Re: about to_path and to_open”
on Fri, 7 Dec 2007 21:19:13 +0900, “Yusuke ENDOH” [email protected]
writes:

|e$B$^$?!“e(Bto_open e$B$N3hMQK!$O!”$A$g$C$H9M$([email protected][email protected]$He(B Uri#to_open e$B$He(B
|e$B%H%j%C%-!<$JM7$S$/$i$$$7$+;W$$$D$+$J$$$s$G$9$,!“B>$K$b2?$+1~MQ$,e(B
|e$B$”$k$s$G$7$g$&$+!#e(B

e$B!VD>@%U%!%$%k$K7k$S$D$$$F$$$J$$$J$K$+$re(B(Stringe$B$G$J$$%%V%8%'e(B
e$B%/%H$K$h$C$F;XDj$7$Fe(B)e$B%
!<%W%s$7$?$$;~!W$J$s$G$9$,!"7k6I$O<gMWe(B
e$B$JMQES$Oe(BURI e$B%/%i%9$K8BDj$5$l$A$c$&$+$b$7$l$^$;$s$M!#e(B

|e$B$"$H!"8=:_e(B Uri#to_open e$B$,<BAu$5$l$F$$$J$$e(B (e$B$G$9$h$M!)e(B) e$B$N$O!“e(B
|e$B2?$+M}M3$,$”$k$s$G$7$g$&$+!#C1$K<j$,[email protected]$1!)e(B

e$B$=$&[email protected]$HG’<1$7$F$$$^$9!#e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs