Ruby find string in file and print result

后端 未结 3 1521
傲寒
傲寒 2020-12-18 23:23

It\'s been a very long time since I\'ve used ruby for things like this but, I forget how to open a file, look for a string, and print what ruby finds. Here is what I have:

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 00:05

    The simplest way to get the root is to do:

    rake routes | grep root
    

    If you want to do it in Ruby, I would go with:

    File.open("config/routes.rb") do |f|
      f.each_line do |line|
        if line =~ /root/
          puts "Found root: #{line}"
        end
      end
    end
    

提交回复
热议问题