0

I wrote the following code in a file 1_arithmetic.rb:

def arithmetic1(n)
  (n * 5) - 20
end

Using the gem tool rake, I typed this into the console:

rake 1_arithmetic.rb:arithmetic1(5) 

Then, I got an error message that reads:

syntax error near unexpected token `('

Does anyone know where I might have done wrong? Or is the problem the way I used rake?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Possible duplicate of [How to pass command line arguments to a rake task](https://stackoverflow.com/questions/825748/how-to-pass-command-line-arguments-to-a-rake-task) – whodini9 Aug 09 '17 at 14:51
  • Why are you using rake at all for this? You don't have a rake task here (AFAICT). Use `irb`, expose the method properly, and take it from there. – Dave Newton Aug 09 '17 at 14:57
  • cheers, i read my tutorial wrong and it was already programmed with a rake task i could call – Etienne Mustow Aug 09 '17 at 15:19

2 Answers2

0

I believe rake can only be used with a rake task. You could create a rake task that then calls your method.

require './1_arithmetic.rb'
task :arithmetic, [:n] => [:environment] do |t, args|
  arithmetic1(args[:n])
end

You would then call the task using

rake arithmetic[5]

reference: How can I run a ruby class from rake file?

reference: How to pass command line arguments to a rake task

Duwayne V.
  • 26
  • 5
  • 2
    How does that parse in Ruby? `require` doesn't need an `end` and `[n]` seems to reference an unknown variable `n`. `[` is also a shell character that might be interpreted, so if there's a file called `arithmetic5` in the directory that's the argument passed in. – tadman Aug 09 '17 at 16:01
  • @engineersmnky That's what I thought. It's a mystery as to why this was accepted. – tadman Aug 09 '17 at 21:29
0

If all you are trying to do is to run that method, you don't need rake to do so, you can just use the ruby command:

ruby -r./1_arithmetic.rb -e "puts arithmetic1(10)"

the -r option tells the ruby command to require the file and the -e option tells ruby to run the code passed in as a string.

Note: The command posted is assuming the file 1_arithmetic.rb is in the directory you're running the command from, you'll need to mess with that file path (./1_arithmetic.rb) if it's somewhere else.

Simple Lime
  • 10,790
  • 2
  • 17
  • 32