Module: OnlyofficeFileHelper::ReadMethods

Included in:
FileHelper
Defined in:
lib/onlyoffice_file_helper/read_methods.rb

Overview

Methods used to read something

Instance Method Summary collapse

Instance Method Details

#read_array_from_file(file_name) ⇒ Array<String>

Read file content to array, split by newline

Parameters:

  • file_name (String)

    path to file

Returns:

  • (Array<String>)

    result of read



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/onlyoffice_file_helper/read_methods.rb', line 24

def read_array_from_file(file_name)
  result_array = []
  return [] unless File.exist?(file_name)

  File.open(file_name, 'r') do |infile|
    while (line = infile.gets)
      result_array << line.sub("\n", '')
    end
  end
  result_array
end

#read_file_to_string(file_name) ⇒ String

Read file content to string

Parameters:

  • file_name (String)

    file to read

Returns:

  • (String)

    result of read



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/onlyoffice_file_helper/read_methods.rb', line 9

def read_file_to_string(file_name)
  result_string = ''
  raise "File not found: #{file_name}" unless File.exist?(file_name)

  File.open(file_name, 'r') do |infile|
    while (line = infile.gets)
      result_string += line
    end
  end
  result_string
end

#read_specific_line(file_name, line_number) ⇒ String

Get line count in file

Parameters:

  • file_name (String)

    name of file

  • line_number (Fixnum)

    line of file to get

Returns:

  • (String)

    line of file by number



40
41
42
43
44
45
# File 'lib/onlyoffice_file_helper/read_methods.rb', line 40

def read_specific_line(file_name, line_number)
  line = `sed '#{line_number + 1}!d' #{file_name}`
  line.chop! if line[-1] == "\n"
  OnlyofficeLoggerHelper.log("Lines in '#{file_name}' by number is '#{line}'")
  line
end