代码之家  ›  专栏  ›  技术社区  ›  kockburn

Ruby launcher.rb不工作

  •  0
  • kockburn  · 技术社区  · 11 年前

    故事

    你好,我决定试试鲁比。我从Ruby官方网站获得了一个关于如何在10分钟内学习Ruby主要概念的链接。 Link here

    本教程直截了当,易于理解。我明白代码应该做什么,但它并没有按预期工作。

    信息:

    1. 不,我运行控制台时没有收到任何错误。
    2. 我在Mac上使用Aptana控制台
    3. 我的 ruby--版本: ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

    代码:

    #!/usr/bin/env ruby
    
    class Test
      def initialize app_map
        @app_map = app_map
      end
      def run file_name
        application = select_app file_name
        system "#{application} #{file_name}"
      end
    
      def select_app file_name
        ftype = file_type file_name
        @app_map[ ftype ]
      end
      def file_type file_name
        File.extname( file_name ).gsub(/^\./, '').downcase
      end
    end
    
    def help
      print " 
      You must pass in the path to the file to launch.
    
      Usage: #{__FILE__} target_file
    " 
    end
    
    if ARGV.empty?
      help
      exit
    end
    
    app_map = {
      'html' => 'firefox',
      'rb' => 'gvim'
    }
    
    test = Test.new app_map
    target = ARGV.join ' '
    test.run target
    

    它可能不明显,但您可以在代码框中向下滚动。

    那么这应该做什么呢?

    $ ruby test.rb test.rb
    

    要运行它,需要添加1个参数。它可能是 .rb或.html (我都试过)。如果是 .html文件 程序应该在其中打开的文件 萤火虫 ,如果是 .rb文件 它应该用 文字编辑器 .

    问题:

    为什么它不起作用?我做错了什么?谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   kockburn    11 年前

    我的终端无法识别firefox应用程序名称。启动程序时未返回任何错误。

    我必须指定完整路径名:

    'html' => 'open /Applications/Firefox.app'
    

    我改变了 gvim vim

    这解决了问题。

    推荐文章