Edu
Edu

Reputation: 1969

Can not open files with special characters using JRuby

The following program works well with ruby but gives me a problem with JRuby when reaching a file with a special character like the one I am using for testing, called "mão.txt":

# coding: utf-8

puts "(A) #{__ENCODING__}"

puts "(B)" + "".encoding.to_s
puts "(C)" + String.new.encoding.to_s

Dir.glob("./fixtures/*").each do |f|
    puts "(D)" + f.encoding.to_s + "  " + f
    File.open(f)
    g = File.expand_path(f)
    puts "(E)" + g + " " + g.encoding.to_s
    File.open(g)
end

The result with JRuby is:

(A) UTF-8
(B)UTF-8
(C)ASCII-8BIT
(D)ASCII-8BIT  ./fixtures/mão.txt~
Errno::ENOENT: No such file or directory - ./fixtures/mão.txt~
  initialize at org/jruby/RubyFile.java:315
        open at org/jruby/RubyIO.java:1176
      (root) at encoding.rb:10
        each at org/jruby/RubyArray.java:1612
      (root) at encoding.rb:8

I am using Ubuntu 12.10, JRuby 1.7.0 and java 1.7.0_09

I plan to have the application packaged with Warble, so I fear command line arguments are not an option.

Upvotes: 0

Views: 402

Answers (2)

Edu
Edu

Reputation: 1969

As Sebastien stated, this a known bug.

I actually found a workaround for this bug. Instead of using Dir.glob, in this case I want every file in the directory, I can simply use Dir.entries and it works fine.

The program can be changed to:

# coding: utf-8
path = File.expand_path(File.dirname(__FILE__))
puts "(A) #{__ENCODING__}"

puts "(B)" + "".encoding.to_s
puts "(C)" + String.new.encoding.to_s

dir = "#{path}/fixtures/"
entries = Dir.entries(dir) - ['.', '..']
entries.each do |f| 
    puts "(D)" + f.encoding.to_s + "  " + f
    file = "#{dir}/#{f}"
    puts "(E)" + file.encoding.to_s + "  " + file
    #f.encode("UTF-8")
    File.open(file)
    g = File.expand_path(file)
    puts "(F)" + g + " " + g.encoding.to_s
    File.open(g)
end

Upvotes: 1

This is a reported bug with Dir.glob.

Upvotes: 1

Related Questions