Skip to content

Instantly share code, notes, and snippets.

@6ewis
Last active December 31, 2015 02:19
Show Gist options
  • Save 6ewis/7919828 to your computer and use it in GitHub Desktop.
Save 6ewis/7919828 to your computer and use it in GitHub Desktop.
random test. # I wanted to try and flush the string before it dies. note that ruby only has a finalizer , it does not have a destructor. I could have use ObjectSpace.define_finalizer self, proc {persist} and use GC.start at the command line to run the garbage collector however it would be after destroying an instance object.
#persistent_string.rb
class PersistentString < String
def initialize data= nil
@@count ||= 0
load data #ObjectSpace.define_finalizer self, proc {persist}
super
end
#Instance Methods
def get_length
data.length
end
def is_palindrome
unless data.split.join.downcase.eql? data.split.join.downcase.reverse #split.join in case the string has \n
puts "The string provided is not a palindrome"
else
puts "The string provided is a palindrome"
end
end
private
attr_accessor :data
def load data
if data.nil?
raise ArgumentError, "no data was provided"
else
self.data = data.to_s
puts "data successfully loaded"
persist
end
end
def persist
File.open "file_#{@@count}.out", 'w+' do |f|
f.puts "#{data}"
end
puts "data entered: #{data}\nfilename: file_#{@@count}.out"
@@count += 1
end
public
#Class methods
class << PersistentString
def read_to file
begin
return STDOUT.puts File.read(file.to_s) if File.exist? file
rescue Exception => e
return error_message(e)
end
error_message
end
def append_to file
begin
if File.exist? file
File.open(file.to_s, 'a') do |f|
puts "Enter data: "
f.write STDIN.gets
end
else
error_message
end
rescue Exception => e
error_message(e)
end
end
private
def error_message(e=nil, incomplete=nil)
STDERR.puts "A dramatic error occured! verify that the file exists"
STDERR.puts "More info about the error: #{e.message}" if e
end
end
end
#This is an example on how the class could be used
2.0.0-p247 :004 > temp = PersistentString.new "This is the beginning"
data successfully loaded
data entered: This is the beginning
filename: file_0.out
=> "This is the beginning"
2.0.0-p247 :005 > temp.get_length
=> 21
2.0.0-p247 :006 > temp.is_palindrome
The string provided is not a palindrome
=> nil
2.0.0-p247 :007 > temp2 = PersistentString.new "This is another random String"
data successfully loaded
data entered: This is another random String
filename: file_1.out
2.0.0-p247 :011 > temp2.index('a')
=> 8
2.0.0-p247 :012 > temp2[8..-1] = "the beginning"
=> "the beginning"
2.0.0-p247 :013 > temp2
=> "This is the beginning"
2.0.0-p247 :014 > temp == temp2
=> true
2.0.0-p247 :015 > PersistentString.read_to [123]
A dramatic error occured! verify that the file exists
More info about the error: no implicit conversion of Array into String
=> nil
2.0.0-p247 :016 > PersistentString.read_to "file_0.out"
This is the beginning
=> nil
2.0.0-p247 :017 > PersistentString.read_to "file_dont_exist"
A dramatic error occured! verify that the file exists
=> nil
2.0.0-p247 :018 > PersistentString.append_to "file_dont_exist"
A dramatic error occured! verify that the file exists
=> nil
2.0.0-p247 :019 > PersistentString.append_to "file_0.out"
Enter data:
THis is the end
=> 16
2.0.0-p247 :020 > PersistentString.read_to "file_0.out"
This is the beginning
THis is the end
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment