Forum: Ruby FSDB to turn file systems into applications

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-14 11:48
(Received via mailing list)
In search for a simple database system that's also an an universal
application service ( similar to Frontier) I've started again to play
around with FSDB. By accident I've found out that this way every file
system can be turned into an application. Using the incredible powerful
"method_missing" construct the following code was implemented:

require 'fsdb'

@@root = FSDB::Database.new('/tmp/examples')
@@path = ''

script = <<MY_SCRIPT
  def show_it(a,b)
    puts a.to_s +  b.to_s
  end
MY_SCRIPT

script2 = <<MY_SCRIPT
  def make_it(a,b)
    puts a + ' : ' + b
  end
MY_SCRIPT

script3 = <<MY_SCRIPT
def hello_method
 write_it("Hello World!")
end
def write_it(txt)
  puts txt
end
MY_SCRIPT

@@root['/scripts/myscripts/show_it'] = script
@@root['/scripts/make_it'] = script2
@@root['/scripts/hello_method'] = script3
@@root['/values/a'] = 25
@@root['/values/b'] = 35
@@root['/values/array'] = [31,2,3,4]


class <<
  def method_missing(method_name,*args )
    @@path << '/' + method_name.to_s
    if !(File.directory?(@@root.absolute("#{@@path}")))
      res = @@root["/#{@@path}"]
      if res.to_s.include? "def"
        if !(methods.include? method_name)
          eval res
          @@path = ''
          return send(method_name,*args)
        end
      end
      @@path = ''
      return res
    end
  end
end


scripts.myscripts.show_it("one","two")
scripts.make_it("one","two")
scripts.hello_method()

puts  values.a + values.b

myVal =   values.array + [5,6,7,8]
puts myVal.to_s

So my question is: has someone else used "method_missing" this way and
are there any "show stoppers" to use "method missing" this way. I'm
planning to use this idea for a very important project and I want to
try to avoid to run into troubles because of a lack of experience.

- Tom
Matthias G. (Guest)
on 2006-03-14 22:09
(Received via mailing list)
> So my question is: has someone else used "method_missing" this way and
> are there any "show stoppers" to use "method missing" this way. I'm
> planning to use this idea for a very important project and I want to
> try to avoid to run into troubles because of a lack of experience.

Generally method_missing magic may lead to infinite recursion loops,
which are quite annoying. I would suggest to allow this kind of magic
only in special blocks, which are evaled in the instance scope of a
database object.

class Database
  def initialize(root)
   @root = FSDB::Database.new(root)
  end
  def method_missing
  ...
  end
  def with(&block)
    instance_eval(&block)
  end
end

db = Database.new("/tmp/examples")

db.with do
  scripts.myscripts.show_it("one","two")
  scripts.make_it("one","two")
  scripts.hello_method()
end

Note, that you lose the instance scope, so methods and instance
variables are not available in the block.

How about recognizing accessor calls for writing to the database?
This topic is locked and can not be replied to.