Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Anders Lund
Anders Lund
3,061 Points

Run Ruby program in ComandLine

I try to run Ruby program in commandLine, and I always get the "`require': cannot load such file -- 00_hello (LoadError)"

Does anyone know how to fix this?

Thanks

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Could you show us the code from that file? It seems that something is wrong inside.

Anders Lund
Anders Lund
3,061 Points

This is the code: it supposed to fail they say, and give an error about the "hello" function, but I get a different error...

require "00_hello"

describe "the hello function" do it "says hello" do hello.should == "Hello!" end end

describe "the greet function" do it "says hello to someone" do greet("Alice").should == "Hello, Alice!" end

it "says hello to someone else" do greet("Bob").should == "Hello, Bob!" end end

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, what tutorial is it? Can you link it?

Anders Lund
Anders Lund
3,061 Points

its not a tutorial, but more like a lesson I got

# Hello!

This lab teaches basic Ruby function syntax.

## Run the test

bundle exec rspec spec/00_hello_spec.rb

## Watch it fail

You should see an error. Don't get scared! Try to read it and

figure out what the computer wants to tell you. Somewhere on the

first line it should say something like

the hello function

says hello (FAILED - 1)

Failures:

1) the hello function says hello

Failure/Error: hello.should == "Hello!"

NameError:

undefined local variable or method `hello' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001009b8808>

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'

## Create the hello function

Fix this by opening lib/00_hello.rb and creating an empty function:

def hello

end

Save it. Run the test again.

## Watch it fail

Now you should see an error like this:

the hello function

says hello (FAILED - 1)

Failures:

1) the hello function says hello

Failure/Error: hello().should == "Hello!"

expected: "Hello!"

got: nil (using ==)

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'

This means that while it found the file, and it found the function,

it's not returning anything! ("nil" is the Ruby way of saying "not

anything".)

## Make it return something

Inside the "hello" function, put a single line containing a string

that is not "Hello!". (Here we are simulating you making an honest

mistake, so we can see what the error message looks like.)

def hello

"whuh?"

end

Save it. Run the test again.

## Watch it fail

Now you should see an error like this:

1) the hello function says hello

Failure/Error: hello().should == "Hello!"

expected: "Hello!"

got: "whuh?" (using ==)

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'

Correct this by changing "whuh?" to "Hello!". Save it. Run the test again.

## Watch it pass!

Hooray! Finally! It works!

## Give yourself a high five

Also, sing a song and do a little dance.

## And then...

Fix the next failure! :-)

the hello function

says hello

the greet function

says hello to someone (FAILED - 1)

In order to get the next test to pass, your function will need to

accept an argument.

def greet(who)

"Hello, #{who}!"

end

require "00_hello"

describe "the hello function" do it "says hello" do hello.should == "Hello!" end end

describe "the greet function" do it "says hello to someone" do greet("Alice").should == "Hello, Alice!" end

it "says hello to someone else" do greet("Bob").should == "Hello, Bob!" end end

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

So do you have the file lib/00_hello.rb? It should be a 00_hello.rb file in the folder called lib.

Anders Lund
Anders Lund
3,061 Points

yes i do have that file

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, try changing this:

require "00_hello"

into this:

require "lib/00_hello"

Anders Lund
Anders Lund
3,061 Points

I ganged it and got the same

ruby /Users/anderslund/Downloads/test-first-ruby-master/spec/00_hello_spec.rb /Users/anderslund/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- lib/00_hello (LoadError) from /Users/anderslund/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /Users/anderslund/Downloads/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<main>' Anderss-MacBook-Pro:Desktop anderslund$

Can you try changing it to require_relative "00_hello"?

Anders Lund
Anders Lund
3,061 Points

tried that too, did not work

Why don't you run it in their environment ...it might work there link and this video might be helpful too.

Anders Lund
Anders Lund
3,061 Points

Gloria, thanks I got it to work through the website :) but I have another question about the Ruby syntax, why do the write "describe" and "it"? I've gone through so many tutorials and read a lot about Ruby but haven't seen those words yet, u know what they mean in the Ruby syntax? thanks

Hi Anders, I am glad you got it to work. It is not actual "ruby syntax". That is why you don't see it there. It is something that has to do with RSpec which is a ruby testing tool. You can read more about it here Basic structure (describe/it) and also have a look at this to learn more on what Rspec is. :)

Anders Lund
Anders Lund
3,061 Points

Thanks you so much for all your help Gloria, really, really awesome!

You are welcome :D

5 Answers

Hello,

What is the name of your file ? If your file is called 'hello.rb', you should run this command :

ruby hello.rb

What exactly do you type before this error appears?

Anders Lund
Anders Lund
3,061 Points

it's called 00_hello_spec.rb

It's a challenge from a tutorial, and when he runs it it says something else in the command line like, can't read the file and so on. Cus I have tried the ruby 00_hello_spec.rb but it gave me the same feedback

Which tutorial is it? Can you explain the process of what you did? What is in 00_hello_spec.rb ?

Anders Lund
Anders Lund
3,061 Points

It's from a school (kind of) I got the file from them (00_hello_spec.rb) In that file it says "Run the file and watch it fail, the error should say something like": 1) the hello function says hello

Failure/Error: hello.should == "Hello!"

NameError:

undefined local variable or method `hello' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001009b8808>

# ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'

Then when I run the file I type: ruby (and then drag the file in) /Users/anderslund/Downloads/test-first-ruby-master/spec/00_hello_spec.rb

And I get the cannot read the file... Error and all that.

Anders Lund
Anders Lund
3,061 Points

Gloria: I type: ruby /Users/anderslund/Downloads/test-first-ruby-master/spec/00_hello_spec.rb

I see. Have you tried doing... cd [dir] till you reach the spec folder and then run the "ruby 00_hello_spec.rb" instead of that path?

Anders Lund
Anders Lund
3,061 Points

Yeah it gives me the same error, cannot load such file. The reason I get the error is not the problem, the problem is that I get the wrong error, cus the program is supposed to fail

Can you try uploading it to your workspace and see if it gives you the same error there?

Anders Lund
Anders Lund
3,061 Points

I'm kind of new to this. what do u mean "uploading it to your workspace" :P

On the panel on the right...where it says... "Home", "Tracks" ... there is something called.. "Workspaces". If you go there you can click "New" and then give it a title ex. "Homework" , an environment "Ruby" ...and in the last option select "Ruby Workspace" that will create a ruby enviroment for you and open a new window with the workspace if you select it. The on the left you will see a folder called "Homework" ... you right click and select "upload file". Then upload your ruby file and run it with ruby 00_hello_spec.rb and see if it runs as expected. If it gives you the same error with the one you have listed to us then it is a problem with the file.

Anders Lund
Anders Lund
3,061 Points

okey, I have uploaded the file in Workspaces, and should i run it in workspaces (how do I do that) or should I run it in the command line? Thanks

There is something called "console" in the workspace. When you are on the workspace you created you will see it. There is an editor above and a console below. You run it in the console.

Anders Lund
Anders Lund
3,061 Points

in the console I don't get anything but /workspace$

I think it's because i don't call the method or anything, since the program is supposed to fail

You have to type what I told you... ruby 00_hello.... to see anything on it. Like you'll normally do on your command line.

Anders Lund
Anders Lund
3,061 Points

yes I got the same error... :P

If you got the same error then it must be an error in the code. XD