[SOLUTION] Whiteout (#34)

Discussion in 'Ruby' started by Ara.T.Howard, Jun 5, 2005.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    my solution tried to strike a balance between being readable and user friendly
    (usage message, etc.) and succicntness. the basic idea is that whiteout.rb is
    a self modifying program that stores the whited-out files in it's __END__
    section as yaml using the expanded path of the original source file as the
    key. this has the nice side effect that all sources remain quite readable
    within the whiteout.rb __END__ section. eg:


    jib:~/tmp > ls
    a.rb b.rb whiteout.rb

    jib:~/tmp > cat whiteout.rb
    #!/usr/bin/env ruby
    require 'yaml'

    this, prog, *paths = [__FILE__, $0, ARGV].flatten.map{|x| File::expand_path x}
    usage = "#{ prog } file [files]+"

    f = open this, 'r+'
    s, pos = f.gets, f.pos until s =~ /^__END__$/
    srcs = YAML::load f

    if prog == this
    abort usage if paths.empty?
    abort "#{ prog } must be writable" unless File::stat(this).writable?
    paths.each do |path|
    s, b = IO::read(path).split(%r/(^\s*#\s*!.*\n)/o).reverse.first 2
    srcs[path] = s
    open(path,'w'){|o| o.puts b, "require 'whiteout'\n"}
    end
    f.seek pos and f << srcs.to_yaml and f.truncate f.pos
    else
    eval srcs[prog]
    end

    __END__
    ---
    {}


    jib:~/tmp > cat a.rb
    #!/usr/bin/env ruby
    p 42


    jib:~/tmp > cat b.rb
    #!/usr/bin/env ruby
    p 'forty-two'


    jib:~/tmp > ruby a.rb
    42


    jib:~/tmp > ruby b.rb
    "forty-two"


    jib:~/tmp > whiteout.rb a.rb b.rb


    jib:~/tmp > cat a.rb
    #!/usr/bin/env ruby
    require 'whiteout'


    jib:~/tmp > cat b.rb
    #!/usr/bin/env ruby
    require 'whiteout'


    jib:~/tmp > ruby a.rb
    42


    jib:~/tmp > ruby b.rb
    "forty-two"


    jib:~/tmp > cat whiteout.rb
    #!/usr/bin/env ruby
    require 'yaml'

    this, prog, *paths = [__FILE__, $0, ARGV].flatten.map{|x| File::expand_path x}
    usage = "#{ prog } file [files]+"

    f = open this, 'r+'
    s, pos = f.gets, f.pos until s =~ /^__END__$/
    srcs = YAML::load f

    if prog == this
    abort usage if paths.empty?
    abort "#{ prog } must be writable" unless File::stat(this).writable?
    paths.each do |path|
    s, b = IO::read(path).split(%r/(^\s*#\s*!.*\n)/o).reverse.first 2
    srcs[path] = s
    open(path,'w'){|o| o.puts b, "require 'whiteout'\n"}
    end
    f.seek pos and f << srcs.to_yaml and f.truncate f.pos
    else
    eval srcs[prog]
    end

    __END__
    ---
    "/home/ahoward/tmp/b.rb": "p 'forty-two'\n"
    "/home/ahoward/tmp/a.rb": "p 42\n"


    all and all quite fun!

    cheers.

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | My religion is very simple. My religion is kindness.
    | --Tenzin Gyatso
    ===============================================================================
     
    Ara.T.Howard, Jun 5, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Francis
    Replies:
    0
    Views:
    451
    Andrew Francis
    Jun 28, 2006
  2. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Solution file not in the solution folder

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Sep 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,154
    Laurent Bugnion
    Sep 12, 2006
  3. Ruby Quiz

    [QUIZ] Whiteout (#34)

    Ruby Quiz, Jun 3, 2005, in forum: Ruby
    Replies:
    9
    Views:
    99
    Matthew D Moss
    Jun 7, 2005
  4. Ryan Leavengood

    [SOLUTION] Whiteout (#34)

    Ryan Leavengood, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    127
    Dominik Bathon
    Jun 6, 2005
  5. Ruby Quiz

    [SUMMARY] Whiteout (#34)

    Ruby Quiz, Jun 9, 2005, in forum: Ruby
    Replies:
    5
    Views:
    109
    Klaus Stein
    Jun 10, 2005
Loading...

Share This Page