Class: Spruz::LinesFile

### Home Classes Methods

### In Files

- lib/spruz/lines_file.rb

### Parent

Object

### Namespace

- MODULE Spruz::LinesFile::LineExtension

### Methods

- ::for_file
- ::for_filename
- ::for_lines
- ::new
- #each
- #empty?
- #file_linenumber
- #inspect
- #last_line_number
- #line
- #line_number=
- #match_backward
- #match_forward
- #next!
- #previous!
- #rewind
- #to_s

### Included Modules

- Enumerable

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Spruz
- Spruz::Attempt
- Spruz::Bijection
- Spruz::Blank
- Spruz::Blank::Array
- Spruz::Blank::FalseClass
- Spruz::Blank::Hash
- Spruz::Blank::NilClass
- Spruz::Blank::Numeric
- Spruz::Blank::Object
- Spruz::Blank::String
- Spruz::Blank::TrueClass
- Spruz::CountBy
- Spruz::DeepDup
- Spruz::FileBinary
- Spruz::FileBinary::ClassMethods
- Spruz::FileBinary::Constants
- Spruz::Full
- Spruz::GO
- Spruz::Generator
- Spruz::HashSymbolizeKeysRecursive
- Spruz::HashUnion
- Spruz::Limited
- Spruz::LinesFile
- Spruz::LinesFile::LineExtension
- Spruz::Memoize
- Spruz::Minimize
- Spruz::ModuleGroup
- Spruz::Null
- Spruz::Once
- Spruz::P
- Spruz::PartialApplication
- Spruz::RangePlus
- Spruz::Round
- Spruz::SecureWrite
- Spruz::Shuffle
- Spruz::StringCamelize
- Spruz::StringUnderscore
- Spruz::StringVersion
- Spruz::StringVersion::Version
- Spruz::Subhash
- Spruz::TimeDummy
- Spruz::TimeDummy::ClassMethods
- Spruz::ToProc
- Spruz::UniqBy
- Spruz::Write
- Array
- Enumerable
- File
- Float
- Hash
- IO
- IRB
- Integer
- Method
- Module
- Object
- Proc
- Range
- String
- Symbol
- Time

No matching classes.

# Spruz::LinesFile

### Attributes

filename[RW]
line_number[R]

### Public Class Methods

for_file(file, line_number = nil) click to toggle source

    # File lib/spruz/lines_file.rb, line 17
    def self.for_file(file, line_number = nil)
      obj = new(file.readlines, line_number)
      obj.filename = file.path
      obj
    end

for_filename(filename, line_number = nil) click to toggle source

    # File lib/spruz/lines_file.rb, line 11
    def self.for_filename(filename, line_number = nil)
      obj = new(File.readlines(filename), line_number)
      obj.filename = filename
      obj
    end

for_lines(lines, line_number = nil) click to toggle source

    # File lib/spruz/lines_file.rb, line 23
    def self.for_lines(lines, line_number = nil)
      new(lines, line_number)
    end

new(lines, line_number = nil) click to toggle source

    # File lib/spruz/lines_file.rb, line 27
    def initialize(lines, line_number = nil)
      @lines = lines
      @lines.each_with_index do |line, i|
        line.extend LineExtension
        line.instance_variable_set :@line_number, i + 1
        line.instance_variable_set :@lines_file, self
      end
      instance_variable_set :@line_number, line_number || (@lines.empty? ? 0 : 1)
    end

### Public Instance Methods

each(&block) click to toggle source

    # File lib/spruz/lines_file.rb, line 73
    def each(&block)
      empty? and return self
      old_line_number = line_number
      1.upto(last_line_number) do |number|
        self.line_number = number
        block.call(line)
      end
      self
    ensure
      self.line_number = old_line_number
    end

empty?() click to toggle source

    # File lib/spruz/lines_file.rb, line 69
    def empty?
      @lines.empty?
    end

file_linenumber() click to toggle source

    # File lib/spruz/lines_file.rb, line 91
    def file_linenumber
      "#{filename}:#{line_number}"
    end

inspect() click to toggle source

    # File lib/spruz/lines_file.rb, line 117
    def inspect
      "#<#{self.class}: #{to_s.inspect}>"
    end

last_line_number() click to toggle source

    # File lib/spruz/lines_file.rb, line 65
    def last_line_number
      @lines.size
    end

line() click to toggle source

    # File lib/spruz/lines_file.rb, line 86
    def line
      index = line_number - 1
      @lines[index] if index >= 0
    end

line_number=(number) click to toggle source

    # File lib/spruz/lines_file.rb, line 58
    def line_number=(number)
      number = number.to_i
      if number > 0 && number <= last_line_number
        @line_number = number
      end
    end

match_backward(regexp, previous_after_match = false) click to toggle source

    # File lib/spruz/lines_file.rb, line 95
    def match_backward(regexp, previous_after_match = false)
      begin
        if line =~ regexp
          previous_after_match and previous!
          return $~.captures
        end
      end while previous!
    end

match_forward(regexp, next_after_match = false) click to toggle source

    # File lib/spruz/lines_file.rb, line 104
    def match_forward(regexp, next_after_match = false)
      begin
        if line =~ regexp
          next_after_match and next!
          return $~.captures
        end
      end while next!
    end

next!() click to toggle source

    # File lib/spruz/lines_file.rb, line 46
    def next!
      old = line_number
      self.line_number += 1
      line_number > old ? self : nil
    end

previous!() click to toggle source

    # File lib/spruz/lines_file.rb, line 52
    def previous!
      old = line_number
      self.line_number -= 1
      line_number < old ? self : nil
    end

rewind() click to toggle source

    # File lib/spruz/lines_file.rb, line 41
    def rewind
      self.line_number = 1
      self
    end

to_s() click to toggle source

    # File lib/spruz/lines_file.rb, line 113
    def to_s
      "#{line_number} #{line.chomp}"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.