[#23231] What do you think about changing the return value of Kernel#require and Kernel#load to the source encoding of the required file? — =?ISO-8859-15?Q?Wolfgang_N=E1dasi-Donner?= <ed.odanow@...>

Dear Ruby developers and users!

8 messages 2009/04/17

[#23318] [Feature #1408] 0.1.to_r not equal to (1/10) — Heesob Park <redmine@...>

Feature #1408: 0.1.to_r not equal to (1/10)

19 messages 2009/04/26

[ruby-core:23244] Re: What do you think about changing the return value of Kernel#require and Kernel#load to the source encoding of the required file?

From: Michael Neumann <mneumann@...>
Date: 2009-04-17 22:03:23 UTC
List: ruby-core #23244
Wolfgang N叩dasi-Donner wrote:

> Wolfgang N叩dasi-Donner schrieb:
>> ...
>> unless (require 'somelib.rb') == __ENCODING__
> I think it may be better to introduce a new method Kernel#lib_encoding.
> It will be called with one parameter, a String containing the library
> name in the same format as for Kernel#load or Kernel#require.
> 
> This method will look for the named library and return an encoding, if
> it is an Ruby source (it has an extension of 'rb' or 'rbw') or nil, if
> it is somethong else (binary).
> 
> This method will not do the work of require/load, it will only take a
> look for the encoding.

You'll be able to implement that in pure Ruby. Basically (needs more 
thought):

  def lib_encoding(file)
    $LOAD_PATH.each {|path|
      fn = File.join(path, file) + ".rb"
      if File.exist?(fn)
        if File.read(fn) =~ /coding:\s*([^ ]*)/
          return $1
        else
          "undefined-encoding"
        end
      end
    }
    return nil
  end
        
But if anyhow possible try to convert all your files to UTF-8 (you probably 
have reasons not to do so). 

Regards,

  Michael



In This Thread