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

T

Thomas Hafner

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
 
D

David Rio

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.

$ cat foo.rb
eval $stdin.read
$ echo "puts 'this is great' "| ruby ./foo.rb
this is great
 
J

Joel VanderWerf

Thomas said:
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.

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)
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top