2

I want to write standalone script in ruby on rails and want to call it in rake task. I have seen lot of post about standalone scripts but nothing seems to clear for me. I am new to this could anyone suggest me how to write these scripts and call them as rake tasks.

I have something like this to run as a script

for example

empty_list = []
    if iterable.is_a?(String)
        i = iterable.chars
        for k in 0..i.length
            if i[k] != i[k + 1]
                empty_list << i[k]
            end
        end
        p empty_list
    else 
        i = iterable
        for k in 0..i.length
            if i[k] != i[k + 1]
                empty_list << i[k]
            end
        end
        p empty_list    
  end

So this should be in my script and run it as a rake task. How to create a script to run this in production environment and where to create this scripts ?

supar
  • 233
  • 1
  • 10
  • http://www.tutorialspoint.com/ruby/ ruby is the language... Rails is a framework made from the language ruby. GLHF! – engineerDave Feb 10 '17 at 16:31
  • 1
    Possible duplicate of [How to use ActiveRecord in a ruby script outside Rails?](http://stackoverflow.com/questions/1643875/how-to-use-activerecord-in-a-ruby-script-outside-rails) – supar Feb 14 '17 at 16:17

1 Answers1

1

You can create a file called a Rakefile with the specific.

What I have here is a Rakefile that assumes your Ruby file (my_file.rb) is in the root directory and you have spec-style tests (Minitest) in a folder called specs. I have two tasks here, one to run the program and another to run the test cases.

The Rakefile is here below:

task :run do
    ruby "my_file.rb"
end

Rake::TestTask.new do |t|
  t.libs = ["lib", "specs"]
  t.warning = false
  t.verbose = false
  t.test_files = FileList['specs/*_spec.rb']
  puts "Running TestTask"
end

task default: :run do
    puts "Running my Rakefile"
end
Chris Mc
  • 75
  • 7