Forum: Ruby load "/proc/self/fd/#{$stdin.fileno}"

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.
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-04-19 02:00
(Received via mailing list)
Hello,

the Ruby code in this article's subject works for my machine running
Ubuntu Linux, but I don't expect it to work for different operating
systems (NetBSD, FreeBSD a.s.o.).

How can alternative OS independent Ruby code look like? Of course, it
shall also load from standard input rather than from a file. Despite
that, it shall be very close to loading a "real" file, e.g.: the line
number of the error location shall be told in case of a syntax error
within the loaded code.

Regards
  Thomas
60c6b87c4cc2716c83a737e0ba2d3bc0?d=identicon&s=25 David Rio (Guest)
on 2009-04-19 02:40
(Received via mailing list)
On Sat, Apr 18, 2009 at 7:00 PM, Thomas Hafner
<thomas@faun.hafner.nl.eu.org> wrote:
> within the loaded code.
$ cat foo.rb
eval $stdin.read
$ echo "puts 'this is great' "| ruby ./foo.rb
this is great
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-04-20 21:33
(Received via mailing list)
Thomas Hafner wrote:
> within the loaded code.
Use #read and #eval instead of #load, and supply the file/line params to
eval:

$ cat bar.rb
puts "in bar"

xfndksnf_syntax_error
$ cat foo.rb
file = "bar.rb"

data = File.read(file)

eval data, nil, "your text here", 0

$ ruby foo.rb
in bar
your text here:2: undefined local variable or method
`xfndksnf_syntax_error' for main:Object (NameError)
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-04-23 00:11
(Received via mailing list)
Thanks to David and Joel!

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