[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. Advertisements

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. =?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,297
    Laurent Bugnion
    Sep 12, 2006
  2. , India
    Replies:
    17
    Views:
    1,383
    James Kanze
    Oct 1, 2007
  3. Replies:
    8
    Views:
    754
  4. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    446
    David Tran
    Jan 10, 2005
  5. Ruby Quiz

    [QUIZ] Whiteout (#34)

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

    [SOLUTION] Whiteout (#34)

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

    [SUMMARY] Whiteout (#34)

    Ruby Quiz, Jun 9, 2005, in forum: Ruby
    Replies:
    5
    Views:
    177
    Klaus Stein
    Jun 10, 2005
  8. Louis J Scoras
    Replies:
    13
    Views:
    521
    Dave Burt
    Oct 5, 2005
Loading...