lib/ole/support.rb in ruby-ole-1.2.11.4 vs lib/ole/support.rb in ruby-ole-1.2.11.5

- old
+ new

@@ -9,18 +9,10 @@ require 'logger' require 'stringio' require 'enumerator' class String # :nodoc: - # plural of String#index. returns all offsets of +string+. rename to indices? - # - # note that it doesn't check for overlapping values. - def indexes string - # in some ways i'm surprised that $~ works properly in this case... - to_enum(:scan, /#{Regexp.quote string}/m).map { $~.begin 0 } - end - def each_chunk size (length / size.to_f).ceil.times { |i| yield self[i * size, size] } end end @@ -163,38 +155,12 @@ recurse.call self, ' ' io end end -# can include File::Constants -class IO - # this is for jruby - include File::Constants unless defined?(RDONLY) - - # nabbed from rubinius, and modified - def self.parse_mode mode - ret = 0 - - case mode[0, 1] - when 'r'; ret |= RDONLY - when 'w'; ret |= WRONLY | CREAT | TRUNC - when 'a'; ret |= WRONLY | CREAT | APPEND - else raise ArgumentError, "illegal access mode #{mode}" - end - - (1...mode.length).each do |i| - case mode[i, 1] - when '+'; ret = (ret & ~(RDONLY | WRONLY)) | RDWR - when 'b'; ret |= Mode::BINARY - else raise ArgumentError, "illegal access mode #{mode}" - end - end - - ret - end - - class Mode +module Ole + class IOMode # ruby 1.9 defines binary as 0, which isn't very helpful. # its 4 in rubinius. no longer using # # BINARY = 0x4 unless defined?(BINARY) # @@ -205,13 +171,35 @@ end include Constants NAMES = %w[rdonly wronly rdwr creat trunc append binary] + # nabbed from rubinius, and modified + def self.parse_mode mode + ret = 0 + + case mode[0, 1] + when 'r'; ret |= RDONLY + when 'w'; ret |= WRONLY | CREAT | TRUNC + when 'a'; ret |= WRONLY | CREAT | APPEND + else raise ArgumentError, "illegal access mode #{mode}" + end + + (1...mode.length).each do |i| + case mode[i, 1] + when '+'; ret = (ret & ~(RDONLY | WRONLY)) | RDWR + when 'b'; ret |= BINARY + else raise ArgumentError, "illegal access mode #{mode}" + end + end + + ret + end + attr_reader :flags def initialize flags - flags = IO.parse_mode flags.to_str if flags.respond_to? :to_str + flags = self.class.parse_mode flags.to_str if flags.respond_to? :to_str raise ArgumentError, "invalid flags - #{flags.inspect}" unless Fixnum === flags @flags = flags end def writeable? @@ -249,10 +237,10 @@ end end =end def inspect - names = NAMES.map { |name| name if (flags & Mode.const_get(name.upcase)) != 0 } + names = NAMES.map { |name| name if (flags & IOMode.const_get(name.upcase)) != 0 } names.unshift 'rdonly' if (flags & 0x3) == 0 "#<#{self.class} #{names.compact * '|'}>" end end end