M
Matt Gretton
Hello all,
What techniques do people generally use to create files and write ruby
code to them?
One way of acheving this (a horrible way) would be just to write the
correct code to a file as follows. (This just creates a file called
file_test.rb that defines a module with two methods)
passed_in_name = "module1"
passed_in_methods = ["meth1","meth2"]
File.open("test_file.rb","w") do |file|
file.puts "module #{passed_in_name}"
file.puts
passed_in_methods.each do |meth|
file.puts "\tdef #{meth}"
file.puts "\t\t\#insert method code here"
file.puts "\tend"
file.puts
end
file.puts "end"
end
I feel there must exist a more elegant way of creating a file liek this.
For example, I have heard that ruby on rails dynamically creates files
containing ruby code. This behavior seems pretty common to various ruby
projects, for example I know rails creates ruby source code dynamically.
Not having had experience with such projects I have tried in vain to
search google for information.
Any advice or generally a point the right direction (to other resources
etc) would be greatly appreciated.
Thanks in advance.
Matt.
What techniques do people generally use to create files and write ruby
code to them?
One way of acheving this (a horrible way) would be just to write the
correct code to a file as follows. (This just creates a file called
file_test.rb that defines a module with two methods)
passed_in_name = "module1"
passed_in_methods = ["meth1","meth2"]
File.open("test_file.rb","w") do |file|
file.puts "module #{passed_in_name}"
file.puts
passed_in_methods.each do |meth|
file.puts "\tdef #{meth}"
file.puts "\t\t\#insert method code here"
file.puts "\tend"
file.puts
end
file.puts "end"
end
I feel there must exist a more elegant way of creating a file liek this.
For example, I have heard that ruby on rails dynamically creates files
containing ruby code. This behavior seems pretty common to various ruby
projects, for example I know rails creates ruby source code dynamically.
Not having had experience with such projects I have tried in vain to
search google for information.
Any advice or generally a point the right direction (to other resources
etc) would be greatly appreciated.
Thanks in advance.
Matt.