Class: IO

Inherits:
Object show all
Extended by:
Nuggets::IO::InteractMixin
Includes:
Nuggets::IO::NullMixin, Nuggets::IO::RedirectMixin
Defined in:
lib/nuggets/io/modes.rb,
lib/nuggets/io/null.rb,
lib/nuggets/io/agrep.rb,
lib/nuggets/io/interact.rb,
lib/nuggets/io/redirect.rb

Overview

#

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

#

Copyright (C) 2007-2011 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@gmail.com>                                       #
                                                                        #

ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU Affero 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 Affero General Public License # for more details. #

#

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

#

++

Constant Summary

Constant Summary

Constants included from Nuggets::IO::NullMixin

Nuggets::IO::NullMixin::NULL

Class Method Summary (collapse)

Methods included from Nuggets::IO::InteractMixin

interact

Methods included from Nuggets::IO::RedirectMixin

#redirect

Class Method Details

+ (Object) _nuggets_original_read



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

alias_method :_nuggets_original_read, :read

+ (Object) agrep(fd, pattern, distance = 0, &block)

call-seq:

IO.agrep(fd, pattern[, distance]) -> anArray
IO.agrep(fd, pattern[, distance]) { |line| ... } -> io


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

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

+ (Object) append(name, binary = false)

call-seq:

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

Opens name with mode a.



76
77
78
# File 'lib/nuggets/io/modes.rb', line 76

def append(name, binary = false)
  open_with_mode(name, 'a', binary, &block_given? ? ::Proc.new : nil)
end

+ (Object) append_read(name, binary = false)

call-seq:

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

Opens name with mode a+.



103
104
105
# File 'lib/nuggets/io/modes.rb', line 103

def append_read(name, binary = false)
  open_with_mode(name, 'a+', binary, &block_given? ? ::Proc.new : nil)
end

+ (Object) read(name, *args)

call-seq:

IO.read(name[, length[, offset]]]) => aString
IO.read(name[, binary]) { |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, &::Proc.new)
end

+ (Object) read_write(name, binary = false)

call-seq:

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

Opens name with mode r+.



85
86
87
# File 'lib/nuggets/io/modes.rb', line 85

def read_write(name, binary = false)
  open_with_mode(name, 'r+', binary, &block_given? ? ::Proc.new : nil)
end

+ (Object) write(name, binary = false)

call-seq:

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

Opens name with mode w.



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

def write(name, binary = false)
  open_with_mode(name, 'w', binary, &block_given? ? ::Proc.new : nil)
end

+ (Object) write_read(name, binary = false)

call-seq:

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

Opens name with mode w+.



94
95
96
# File 'lib/nuggets/io/modes.rb', line 94

def write_read(name, binary = false)
  open_with_mode(name, 'w+', binary, &block_given? ? ::Proc.new : nil)
end