Class: IO

Inherits:
Object show all
Defined in:
lib/nuggets/io/modes.rb,
lib/nuggets/io/agrep.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright © 2007-2008 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@uni-koeln.de>                                    #
                                                                        #

ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. #

#

You should have received a copy of the GNU General Public License along # with ruby-nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Class Method Summary collapse

Class Method Details

._nuggets_original_readObject



34
# File 'lib/nuggets/io/modes.rb', line 34

alias_method :_nuggets_original_read, :read

.agrep(fd, pattern, distance) ⇒ Object

call-seq:

IO.agrep(fd, pattern[, distance]) => anArray


35
36
37
# File 'lib/nuggets/io/agrep.rb', line 35

def self.agrep(fd, pattern, distance)
  open(fd) { |io| io.agrep(pattern, distance) }
end

.append(name, binary = false) ⇒ Object

call-seq:

IO.append(name, binary = false) => anIO
IO.append(name, binary = false) { |io| ... } => anObject

Opens name with mode a.



78
79
80
81
82
# File 'lib/nuggets/io/modes.rb', line 78

def append(name, binary = false)
  block_given? ?
    open_with_mode(name, 'a', binary) { |*a| yield(*a) } :
    open_with_mode(name, 'a', binary)
end

.append_read(name, binary = false) ⇒ Object

call-seq:

IO.append_read(name, binary = false) => anIO
IO.append_read(name, binary = false) { |io| ... } => anObject

Opens name with mode a+.



111
112
113
114
115
# File 'lib/nuggets/io/modes.rb', line 111

def append_read(name, binary = false)
  block_given? ?
    open_with_mode(name, 'a+', binary) { |*a| yield(*a) } :
    open_with_mode(name, 'a+', binary)
end

.read(name, *args) ⇒ Object

call-seq:

IO.read(name, [length [, offset]]) => aString
IO.read(name, binary = false) { |io| ... } => anObject

Opens name with mode r. NOTE: With no associated block, acts like the original IO::read, not like IO::new.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nuggets/io/modes.rb', line 42

def read(name, *args)
  return _nuggets_original_read(name, *args) unless block_given?

  case args.size
    when 0
      # ok
    when 1
      case binary = args.first
        when true, false, nil
          # ok
        else
          raise TypeError, "wrong argument type #{binary.class} (expected boolean)"
      end
    else
      raise ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
  end

  open_with_mode(name, 'r', binary) { |*a| yield(*a) }
end

.read_write(name, binary = false) ⇒ Object

call-seq:

IO.read_write(name, binary = false) => anIO
IO.read_write(name, binary = false) { |io| ... } => anObject

Opens name with mode r+.



89
90
91
92
93
# File 'lib/nuggets/io/modes.rb', line 89

def read_write(name, binary = false)
  block_given? ?
    open_with_mode(name, 'r+', binary) { |*a| yield(*a) } :
    open_with_mode(name, 'r+', binary)
end

.write(name, binary = false) ⇒ Object

call-seq:

IO.write(name, binary = false) => anIO
IO.write(name, binary = false) { |io| ... } => anObject

Opens name with mode w.



67
68
69
70
71
# File 'lib/nuggets/io/modes.rb', line 67

def write(name, binary = false)
  block_given? ?
    open_with_mode(name, 'w', binary) { |*a| yield(*a) } :
    open_with_mode(name, 'w', binary)
end

.write_read(name, binary = false) ⇒ Object

call-seq:

IO.write_read(name, binary = false) => anIO
IO.write_read(name, binary = false) { |io| ... } => anObject

Opens name with mode w+.



100
101
102
103
104
# File 'lib/nuggets/io/modes.rb', line 100

def write_read(name, binary = false)
  block_given? ?
    open_with_mode(name, 'w+', binary) { |*a| yield(*a) } :
    open_with_mode(name, 'w+', binary)
end