Ruby Simple Read/Write File (Copy File) -
i practicing ruby, , trying copy contents file "from" file "to". can tell me did wrong?
thanks !
from = "1.txt" = "2.txt" data = open(from).read out = open(to, 'w') out.write(data) out.close data.close
maybe missing point, think writing more 'ruby'
from = "1.txt" = "2.txt" contents = file.open(from, 'r').read file.open(to, 'w').write(contents)
personally, however, use operating systems terminal file operations so. here example on linux.
from = "1.txt" = "2.txt" system("cp #{from} #{to}")
and windows believe use..
from = "1.txt" = "2.txt" system("copy #{from} #{to}")
finally, if needing output of command sort of logging or other reason, use backticks.
#a nice 1 liner `cp 1.txt 2.txt`
here system , backtick methods documentation. http://ruby-doc.org/core-1.9.3/kernel.html
Comments
Post a Comment