Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby get line from a file

# open the file
File.open('foo.txt') do |file|
  # iterate over each line with its index
  file.each_line.with_index(1) do |line, number|
    puts "#{number}: #{line}"
  end
end
# since we passed a block, the file is automatically closed after `end`
Comment

ruby get line from a file

# open the file
File.open('foo.txt') do |file|
  # iterate over each line
  file.each_line do |line|
    puts line
  end
end
# since we passed a block, the file is automatically closed after `end`
Comment

ruby get line from a file

# open the file
file = File.open('foo.txt')

# iterate over each line
file.each_line do |line|
  puts line
end

# close the file afterwards
file.close
Comment

ruby read file line by line

File.readlines('foo').each do |line|
	puts line
end
Comment

PREVIOUS NEXT
Code Example
Ruby :: rails task arguments 
Ruby :: in query postgres jsonb rails 
Ruby :: rspec parallel tests 
Ruby :: ruby delete folder recursively 
Ruby :: rspec add support folder 
Ruby :: activerecord less than 
Ruby :: create rails project with postgres 
Ruby :: max keys from hash ruby 
Ruby :: ruby check if a file exists 
Ruby :: rails remove model 
Ruby :: rails validate email 
Ruby :: remove gem rails 
Ruby :: array string ruby 
Ruby :: ruby pop array 
Ruby :: ruby array prepend vs unshift 
Ruby :: rails disable submit button 
Ruby :: read xls file in ruby 
Ruby :: rails secure uuid 
Ruby :: read headers of csv using ruby 
Ruby :: rspec gem tutorial 
Ruby :: ruby for 
Ruby :: ruby deep copy 
Ruby :: stripe test keys 
Ruby :: formatting a floating point number in ruby 
Ruby :: expect actionmailer base nullmail 
Ruby :: rails faker address 
Ruby :: my rails server exits automatically 
Ruby :: Ruby deep clone with Marshal 
Ruby :: add key and value to the beginning of a hash ruby 
Ruby :: what is ruby 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =