Dan*_*Tao 40 ruby string io file
我相信这很容易; 我无法立即从谷歌找到答案.
我知道我能做到这一点(对吗?):
text = ""
File.open(path).each_line do |line|
text += line
end
# Do something with text
Run Code Online (Sandbox Code Playgroud)
但那似乎有点过分,不是吗?或者这是人们在Ruby中做到的方式?
Thi*_*ira 133
IO.read()正是你要找的.
File是IO的子类,所以你也可以使用:
text = File.read(path)
Run Code Online (Sandbox Code Playgroud)
不能比这更直观.