如何从rake文件中运行ruby类? [英] How can I run a ruby class from rake file?

查看:187
本文介绍了如何从rake文件中运行ruby类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 sample.rake 文件运行ruby类.

I want to run a ruby class from a sample.rake file.

考虑 myruby.rb 是红宝石文件.

我想像ruby myruby.rb

推荐答案

在@tobias必须说的内容中添加示例脚本

Adding to what @tobias has to say here you go with an example script

myruby.rb

puts "hello world"

创建名为Rakefile

task :default => [:test]

task :test do
    ruby "my_file.rb"
end

现在,如果您调用rake,它将在控制台中记录hello world文本.

Now if you invoke rake it should file up hello world text in console.

更新

如果按照@tobias的建议将调用包装到函数调用中,那将更有意义

It would make more sense if you wrap your call in a function call as suggested already by @tobias

所以您的Rakefile会变成

require './myruby.rb'
task :default => [:test]

task :test do
  ruby "my_file.rb"
end

task :test2 do
  do_something
end

myruby.rb

def do_something
  puts "do something"
end

现在rake test2应该吐出do something

这篇关于如何从rake文件中运行ruby类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆