Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created May 1, 2014 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinokada/11464301 to your computer and use it in GitHub Desktop.
Save shinokada/11464301 to your computer and use it in GitHub Desktop.
def readfile1(file_name)
# read a file and returns string "17 10 16 3 7 "
file = File.open(file_name)
contents = file.read.gsub(/\r\n/, ' ')
end
def readfile2(file_nam)
# same as above
File.readlines(file_name).map(&:chomp).join(' ')
end
def readfile3(file_name)
File.readlines(file_name).map(&:chomp) # ["17", "10", "16", "3", "7"]
.join(' ') # "17 10 16 3 7"
end
# by using the above method by spliting gives each value
def reafile3a(file_name)
# read a file and map(&:chomp) will give a string
# and gives 4 items, hol, jap, math max_jap values etc.
hol, jap, math, max_jap, max_math= File.readlines(file_name).map(&:chomp).join(' ').split(' ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment