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 ?

2

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.