use of jruby causes "Errno::EBADF: Bad file descriptor" error - ruby

I've got a little executable we'll call "decode" (written in C) that takes a block of data on stdin (an image file), converts it, and spits it back to stdout. So from the linux command line the following command works just fine:
cat image_file.format1 | ./irb/decode > image_file.format2
I'm trying to wrap this binary in some ruby code and eliminate the need for the use of regular file IO using Open3.popen3. Here's the relevant section of Ruby code:
.
.
.
Open3.popen3("decode") do |stdin, stdout, stderr, wait_thr|
stdin.write(f)
stdin.close_write
#image_data = stdout.read
#status_message = stderr.read
exit_status = wait_thr.value
end
.
.
.
Variable f contains the block of data to convert. I was writing it to file and then calling decode on the written file. When trying to run the the above code from irb using jruby, one gets the following error traceback (slightly sanitized):
Traceback (most recent call last):
16: from org/jruby/RubyKernel.java:1180:in `catch'
15: from org/jruby/RubyKernel.java:1418:in `loop'
14: from org/jruby/RubyKernel.java:1037:in `eval'
13: from t:5:in `<eval>'
12: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:46:in `process'
11: from org/jruby/RubyKernel.java:311:in `open'
10: from org/jruby/RubyIO.java:1179:in `open'
9: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:63:in `block in process'
8: from org/jruby/ext/stringio/StringIO.java:423:in `each_byte'
7: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:87:in `block in process'
6: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:205:in `decodeTiff'
5: from /home/user/dev/kf_decoder/lib/kf_decoder/tiff.rb:123:in `initialize'
4: from /home/user/.rbenv/versions/jruby-9.2.0.0/lib/ruby/stdlib/open3.rb:102:in `popen3'
3: from /home/user/.rbenv/versions/jruby-9.2.0.0/lib/ruby/stdlib/open3.rb:206:in `popen_run'
2: from org/jruby/RubyKernel.java:1642:in `spawn'
1: from org/jruby/RubyProcess.java:1570:in `spawn'
Errno::EBADF (Bad file descriptor - irb/decode
The funny thing is that the exact same code works fine unchanged in irb if I'm using the system ruby interpreter, or rubinius (both of which I have installed and can switch between using rbenv).
Can anyone tell me what gives? I'm runing ubuntu linux 18.04 LTS, and jruby 9.2.0.0 (2.5.0). Jruby is the platform of choice because of speed and other considerations, so I need to get this working.

The problem has to do with the version of java that jruby uses. As of this posting java 9 and 10 don't work with jruby and popen3 the way I needed to use it. Java 8 does. For complete details, see the following thread on Github:
https://github.com/jruby/jruby/issues/5249

Related

How could a line of code cause Ruby to not recognize a - sign?

While executing this line:
if behavior[i,0] < Thrsh and theStim == "small" then fitness[0,i]-= 4 end
I get this error
EvoCat.rb:51:in `block (2 levels) in <main>': undefined method `-' for nil:NilClass (NoMethodError)
Most of it is copied from a program that runs fine, only 'theStim == "small"' is new but that seems Ok. I can't understand how I could do anything that would cause Ruby to no longer recognize a - sign. If it's not the line of code any hints where I should look for the problem would be appreciated.

Rubymine file reference

I'm trying to reference a file to write to it using RubyMine and I'm having trouble figuring it out. When using the full path, the code errors out while running it with RubyMine.
When I use the same code and run it in terminal, the code works fine using the command:
ruby studio_game players.csv
How can I get the file to be recognized without having to designate the full path in RubyMine?
Erring Code:
require_relative 'player'
require_relative 'game'
player1 = Player.new("moe")
player2 = Player.new("larry", 60)
player3 = Player.new("curly", 125)
knuckleheads = Game.new("Knuckleheads")
knuckleheads.load_players(ARGV.shift || 'players.csv')
Error message:
/Users/MNickey/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) "/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game"
/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `readlines': No such file or directory - players.csv (Errno::ENOENT)
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `load_players'
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game:9:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
knuckleheads.load_players(ARGV.shift || 'players.csv') is almost certainly what is causing the problem. Unless you have configured the launcher for the script with a command line argument, ARGV will be empty and you will be getting plain-old 'players.csv' as the result.
I'm pretty certain you are running the script with an argument when you run it in the command line.
To set a command line parameter in RubyMine, go to Run / Edit Configurations, find the launch configuration for your script, and add the path to your csv file to the Script Arguments input box.

Problems running rake with rspec and ruby

I am trying to run rake on a ruby file where I am supposed to receive an error so I can debug it as an exercise. However the error I get is not the error Im supposed to receive. I'm receiving the following and I'm having a time interpreting what I need to fix.
~Desktop/learn_ruby-master/00_hello$ rake
(in /~Desktop/learn_ruby-master)
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- hello (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ~Desktop/learn_ruby-master/00_hello/hello_spec.rb:117:in `<top (required)>'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
rake aborted!
/usr/bin/ruby1.9.1 -S rspec ~Desktop/learn_ruby-master/00_hello/hello_spec.rb - I/~Desktop/learn_ruby-master/00_hello -I/~Desktop/learn_ruby-master/00_hello/solution -f documentation -r ./rspec_config failed
Tasks: TOP => default => spec
( See full trace by running task with --trace)
Here is the code I am running rake on
require "hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
I had the same problem (going through the exact same tutorial). I just spent three days of my life trying to figure it out. The issue was that one of my folders in the path to 'hello.rb' had a space in between two words. Seriously, that was it. The ruby path couldn't pick it up no matter what I did (except change the space). Uggh. Lesson learned, don't name anything, anywhere with a space from here on out.
Ruby says it all: "Cannot find filename hello.rb on the library loading path". You are missing hello.rb file or Ruby cannot find it. Do you really have it on the disc in the directory you run rake from? If its somewhere else you need to provide relative path.
Also remove the whitespace from the first line, I suspect you have some gargabe there. There should be only one space between -- and hello in the error message.

Running my first RSpec test, is there any way to print just the error with no backtrace?

I'm looking at this tutorial online:
https://www.relishapp.com/rspec/docs/gettingstarted
# game_spec.rb
describe Game do
describe "#score" do
it "returns 0 for all gutter game" do
game = Game.new
20.times { game.roll(0) }
game.score.should == 0
end
end
end
Running the first example, I noticed that the error message they list is much shorter than what I'm seeing. On my terminal the stack trace is so long it scrolls off the top. Is there any way to shorter error message / stack output to make this fit?
/testspec/game_spec.rb:3:in `<top (required)>': uninitialized constant Game (NameError)
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
I stripped my home dir from the list of dirs there. Is it possible to order verbosity down to simply the NameError line?
As I guessed in the comment, there is an error with your configuration/installation.
`require': cannot load such file -- ./game
Ruby can not find that file. Make sure you have the correct file in place, and run the test again.

Ruby Net::SFTP does not work, even on its own example code

Trying to use Net::SFTP, version 2.05 (appears to be the latest). But it fails even on its own sample code (below, from http://net-ssh.rubyforge.org/sftp/v2/api/index.html). The domain, user and password are the same as I use to log in manually. I will change the paths when I can get that far.
Net::SFTP.start('ftp.domain.com', 'user', :password => 'password') do |sftp|
# upload a file or directory to the remote host
sftp.upload!("/path/to/local", "/path/to/remote")
# download a file or directory from the remote host
sftp.download!("/path/to/remote", "/path/to/local")
# grab data off the remote host directly to a buffer
data = sftp.download!("/path/to/remote")
# open and write to a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "w") do |f|
f.puts "Hello, world!\n"
end
# open and read from a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "r") do |f|
puts f.gets
end
# create a directory
sftp.mkdir! "/path/to/directory"
# list the entries in a directory
sftp.dir.foreach("/html") do |entry|
puts entry.longname
end
end
It hangs on the first line, and the traceback is below (line 14 is the Net::SFTP.start line)...
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:801:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:212:in `preprocess'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:197:in `process'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:35:in `start'
C:/Data/dev/Ruby/RubyProjects/FTP_test/lib/ftp_test.rb:14:in `<top (required)>'
Windows 7, netbeans 6.9.1, ruby 1.9.2. The sample code is from the readme at..
When i used Net::SFTP in Perl i faced similar issue and its becos ssh path was not set properly.
Can you check whether SSH is in path.
I know the answer is not related to Ruby but suggested this as a general observation.

Resources