'require' is not recognised

T

Tara Keane

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?
 
N

Nathan Clark

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?

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).
 
T

Tara Keane

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 <NameError>

I thought I could enter that code direct into IRB?
 
J

Jeremy Bopp

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 <NameError>

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
 
T

Tara Keane

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.
 
J

Jeremy Bopp

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

You need to put quotes around benchmark in your require statement. The
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.
 
T

Tara Keane

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?
 
J

Jeremy Bopp

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?

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
 
J

Jesús Gabriel y Galán

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 =3D 100000
a=3DBenchmark.measure do
=A0for i in 1..iterations do
=A0x=3Di
=A0end
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: http://www.ruby-lang.org/en/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 =3D 100000
iterations.times do
# your code
end

Jesus.
 
T

Ted Flethuseo

You can also add:

#!/usr/bin/env ruby

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

Ted
 
J

Jeremy Bopp

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
 
T

Tara Keane

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top