![](/img/trans.png)
[英]Stubbing write method inside File.open using Mocha in Rails
[英]File.open, write and save?
我正在嘗試獲取一個.rb文件,以便在該文件運行時在具有指定內容的特定目錄中創建另一個.rb文件。 我不知道最好的方法是使用Ruby文件還是Rake文件。 您的輸入將會很棒。
如果您只需要執行一個簡單的腳本(如創建文件),則只需使用Ruby腳本,而無需創建rake任務。
# file origin.rb
target = "target.rb"
content = <<-RUBY
puts "I'm the target!"
RUBY
File.open(target, "w+") do |f|
f.write(content)
end
您可以使用以下命令執行文件
$ ruby origin.rb
directory = "../../directory"
File.open(File.join(directory, 'file.rb'), 'w') do |f|
f.puts "contents"
end
事實證明這是最好的解決方案。
File.open("linecount.txt",'w') do |filea|
File.open("testfile.txt",'r') do |fileb|
while line = fileb.gets
filea.puts line.length
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.