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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大