'require' is not recognised

New to Ruby and trying to run benchmark
I got an error that ‘require’ is not recognised as an internal or
external command, operable program or batch file
Does it have something to do with not having the right gems installed?
If so how do I do that?

How do you try to run that file? Sounds like your shell is trying to
evaluate the source instead of passing it to ruby.
Try:
ruby your-file.rb
Instead of:
./your-file.rb

On Tue, Nov 30, 2010 at 8:18 AM, Tara K. [email protected]
wrote:

New to Ruby and trying to run benchmark
I got an error that ‘require’ is not recognised as an internal or
external command, operable program or batch file
Does it have something to do with not having the right gems installed?
If so how do I do that?


Posted via http://www.ruby-forum.com/.

It sounds like you’re entering Ruby code directly into the command
prompt. Try entering your code into a separate file (“something.rb”)
and run “C:\Ruby192\bin\ruby.exe something.rb” to have the Ruby
interpreter run your code (the path to the Ruby interpreter on your
system will likely be different).

Thanks - I created a file with the following code which I ran from cmd
prompt

require benchmark
puts Benchmark.measure{10000.times{print"."}}

Now I’m getting an error 'undefined local variable or method ‘benchmark’
for main: Object

I thought I could enter that code direct into IRB?

On 11/30/2010 8:05 AM, Tara K. wrote:

Thanks - I created a file with the following code which I ran from cmd
prompt

require benchmark
puts Benchmark.measure{10000.times{print"."}}

Now I’m getting an error 'undefined local variable or method ‘benchmark’
for main: Object

require “benchmark”

I thought I could enter that code direct into IRB?

Yes, you can do that, but you haven’t mentioned using irb yet. If
that’s what you thought you were doing earlier when you got that error
message concerning require, you must have neglected to actually start
irb in your cmd prompt session.

You can start irb by running irb.bat. That should be found within the
bin directory of your Ruby installation.

-Jeremy

Sorry if I wasn’t specific before
I’m in irb and as soon as I type the first line of code i.e. require
benchmark I get the error 'undefined local variable or method
‘benchmark’ for main:Object

Then I tried saving all the code in a text file and running it from cmd
prompt with Ruby test.rb and I got the same error.

On 11/30/2010 8:36 AM, Tara K. wrote:

Sorry if I wasn’t specific before
I’m in irb and as soon as I type the first line of code i.e. require
benchmark I get the error 'undefined local variable or method
‘benchmark’ for main:Object

Then I tried saving all the code in a text file and running it from cmd
prompt with Ruby test.rb and I got the same error.

You need to put quotes around benchmark in your require statement. The
require method takes a string:

require “benchmark”

-Jeremy

On Tue, Nov 30, 2010 at 8:38 AM, Jeremy B. [email protected] wrote:

require method takes a string:

require “benchmark”

-Jeremy

And “string” is just fancy programmer speak for “text” (data composed of
sequential characters). And you define text by wrapping it in quotation
marks, hence, as Jeremy said:

require “benchmark”

require is code – it tells the interpreter to call the method named
require
(methods are just pieces of code that have been named, and can be
executed
over and over).

“benchmark” is data – the interpreter knows it is text (a string) and
gives
it to the require method.

thanks for your help - I had forgotten to include benchmark inside
quotes so it wasn’t picking it up.
one more quick question…

require’benchmark’
iterations = 100000
a=Benchmark.measure do
for i in 1…iterations do
x=i
end
end

In the above code (from Beginning Ruby book)
what’s the difference between benchmark and Benchmark(uppercase)
Also why 2 periods 1…iterations?

On 11/30/2010 10:00 AM, Tara K. wrote:

end

In the above code (from Beginning Ruby book)
what’s the difference between benchmark and Benchmark(uppercase)
Also why 2 periods 1…iterations?

The name benchmark is the name of the file to load which in this case
contains the definition of the Benchmark class/module. Until you load
the benchmark file, the Benchmark class/module does not exist in your
Ruby session. Benchmark is uppercase because as a class or module it is
a constant, and Ruby requires that constants’ names begin with upper
case letters.

The 1…iterations statement defines a Range object from 1 to the value
of iterations (100000 in this case). The for statement iterates through
that range, setting i to each value and calling the block each time.
There is a similar statement, 1…iterations (note the 3 dots), that
also defines a Range object. The … form creates an inclusive range
while the … form creates an exclusive range from 1 to iterations - 1:

(1…2).to_a.last #=> 2
(1…2).to_a.last #=> 1

-Jeremy

On Tue, Nov 30, 2010 at 5:00 PM, Tara K. [email protected]
wrote:

end

In the above code (from Beginning Ruby book)
what’s the difference between benchmark and Benchmark(uppercase)

I recommend you read an introduction to Ruby (there are some useful
links here: Documentation).

When you do require ‘benchmark’, the require method would search for a
suitable file in the load path (benchmark.rb, benchmark.so, etc
depending on the OS). When it finds the file, it loads its contents
and execute them. In this case, the benchmark.rb file creates a
constant called Benchmark, which you can then use. It has a method
measure you can call and so on.

Also why 2 periods 1…iterations?

a…b is a Range literal. That for syntax executes the block once for
each value between 1 and iterations. Take a look at Ranges to
understand.

By the way, I prefer to do this kind of things like this:

iterations = 100000
iterations.times do

your code

end

Jesus.

On 11/30/2010 10:30 AM, Ted F. wrote:

You can also add:

#!/usr/bin/env ruby

at the beginning of your file, and then you can call
it as an executable.

That will not work for Windows out of the box, and Windows is where Tara
is working. On Windows you usually create a file association for the
.rb extension that opens such files with ruby.exe.

-Jeremy

You can also add:

#!/usr/bin/env ruby

at the beginning of your file, and then you can call
it as an executable.

Ted

Thanks all - I’m sure I’ll have loads more questions in future.
Good to know there’s such great support out there for the newbies :slight_smile: