404 2010-01-15 16:38
浏览 236
已采纳

ruby文件操作

require 'app/configuration'
module RubyAMF
  module Configuration
    #set the service path used in all requests
    # RubyAMF::App::RequestStore.service_path = File.expand_path(RAILS_ROOT) + '/app/controllers'

    # => CLASS MAPPING CONFIGURATION
    ClassMappings.register(
      :actionscript  => 'Stock',
      :ruby          => 'Stock',
      :type          => 'active_record',
      :attributes    => ["code", "name"])
    end
end 

 上面算是整个文件吧,之后我想在另一个同等级路径文件下要操作这个文件

File.open("rubyamf_config.rb") do |file|
    while line = file.gets
      unless line.include?("#")
      line.scan(//)
        print line
      end
    end
    file.close
  end 

怎么操作呢,1.不要注释,之后把下面的代码写到一个数组里,如最下面的模式

ClassMappings.register(
      :actionscript  => 'Stock',
      :ruby          => 'Stock',
      :type          => 'active_record',
      :attributes    => ["code", "name"])

#模式:
{'ClassMappings.register' => ['actionscript' => 'Stock', 'ruby' => 'Stock', 'type' => 'active_record', 'attributes' => ["code", "name"]]}

ruby操作这些玩意还不会呢!哎!如能回答,另加完我得分,不知道javaeye能不能追加?

 

 

 

 

 

 

 

  • 写回答

2条回答 默认 最新

  • Hooopo 2010-01-15 18:30
    关注

    [code="ruby"]

    content = File.read("rubyamf_config.rb")
    result =content.gsub(/#.*$/,"").sub(/ClassMappings.register(\s+(.*?)end/m) do
    "{" +
    'ClassMappings.register'.inspect +
    " => " +
    "[" +
    eval("{#{$1.sub(/\s+$/,"").chop}}").map{|k,v| k.to_s.inspect + " => " + v.inspect}.join(", ") +
    "]" +
    "}"
    end
    File.open("rubyamf_config.rb", "w+"){|f| f.write result.gsub("\"","'")}
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?