summaryrefslogtreecommitdiff
path: root/ruby/to_file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ruby/to_file.rb')
-rwxr-xr-xruby/to_file.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/ruby/to_file.rb b/ruby/to_file.rb
new file mode 100755
index 0000000..5b6967f
--- /dev/null
+++ b/ruby/to_file.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+
+module ToFile
+ def filename
+ "object_#{self.object_id}.txt"
+ end
+ def to_f
+ File.open(filename, 'w') {|f| f.write(to_s)}
+ end
+end
+
+class Person
+ include ToFile
+ attr_accessor :name
+
+ def initialize(name)
+ @name = name
+ end
+ def to_s
+ name
+ end
+end
+
+Person.new("matz").to_f