12 Replies - 1141 Views - Last Post: 18 June 2013 - 01:52 PM Rate Topic: -----

#1 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 10:30 AM

puts 1+2


This is an excercise from Chris Pine's book "Learn to Program". Ive searched high and low on the web for a straight answer but nothing seems to work. I have ruby installed. I'm in the right directory and I still get an error message that states"ruby: No such file or directory -- calc.rb (LoadError)". I named the program ruby calc.rb. I've moved to the correct directory by typing in "cd Desktop" and the "cd programs" which is where I saved the ruby calc.rb file. I then go to my terminal, type in "ruby calc.rb" as directed and receive the above error. Its getting very frustrating because it appears that every solution I find on any website at all doesn't work for me. Help on this would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: puts 1+2 help (not executing)(newb)

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 10:46 AM

sounds like more of a linux issue than a ruby issue, maybe.

Can you do the following:

>> cd Desktop/programs
>> ls -l calc.rb
>> which ruby
>> ruby calc.rb


and post the result
Was This Post Helpful? 1
  • +
  • -

#3 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 10:57 AM

Sure thing.

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cd Desktop/programs
Mark-and-Ashley-Hustads-MacBook-Pro:programs mahustad$ ls -l calc.rb
ls: calc.rb: No such file or directory
Mark-and-Ashley-Hustads-MacBook-Pro:programs mahustad$ which ruby
/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby
Mark-and-Ashley-Hustads-MacBook-Pro:programs mahustad$ ruby calc.rb
ruby: No such file or directory -- calc.rb (LoadError)

Hope thats what you were asking for. Thanks

I even uninstalled and then reinstalled rails.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:01 AM

Mark-and-Ashley-Hustads-MacBook-Pro:programs mahustad$ ls -l calc.rb
ls: calc.rb: No such file or directory


Okay, so calc.rb is not found in that directory.

Try this:

>> cat > calc.rb
puts 1 + 2
^D
>> ruby calc.rb


(The ^D means "to terminate the "cat" type CTRL-D at the start of a blank line" - don't type the characters caret and D!)
Was This Post Helpful? 2
  • +
  • -

#5 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:25 AM

ok, so heres everything I just did.

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > calc.rb
puts 1 + 2
ruby calc.rb

Thats all that happened

Just did this as well. Wasn't sure if I was supposed to put it all in the same line.


Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > calc.rb puts 1 + 2
cat: puts: No such file or directory
cat: 1: No such file or directory
cat: +: No such file or directory
cat: 2: No such file or directory
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ ruby calc.rb
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:30 AM

Okay, that should have worked, so let's review.

Does calc.rb exist now?

>> ls -l calc.rb


If it exists, what does it have in it?

>> more calc.rb



I'm pretty sure ruby is working on your system, but you can test it:

>> ruby --version

should return a version number and patchlevel - if this happens, then ruby is installed and should work. You can then test a calculation. If you just run "ruby" from the command line, it'll accept commands until you send the EOF marker (CTRL-D again) on a blank line. It will then execute those commands.

So you can do:

>> ruby
puts 1+2
^D

and it should print 3 and exit.
Was This Post Helpful? 3
  • +
  • -

#7 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:43 AM

Here my last few lines

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ ruby
puts 1+2
3D
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ ruby
puts 1+2
3D
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ ruby --version
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0]
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$


The "3D" showed up after I typed in "CNTRL - D" I did it twice to double check if that was why. I then checked the version.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:50 AM

View Posthustada, on 18 June 2013 - 01:25 PM, said:

Just did this as well. Wasn't sure if I was supposed to put it all in the same line.


Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > calc.rb puts 1 + 2
cat: puts: No such file or directory
cat: 1: No such file or directory
cat: +: No such file or directory
cat: 2: No such file or directory
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ ruby calc.rb



We should probably get you some review on the bash shell at some point. :)

cat just takes from the standard input and gives to the standard output. > is a redirect. It means "for the moment, use this filehandle as the standard output and send stuff there". calc.rb is of course the name of a file. Redirecting to it will create it if it doesn't already exist.

So if you put those together, you get

>> cat > calc.rb

meaning, "take whatever comes in on the standard input and send it to the standard output, which for the moment is the file calc.rb" So you hit return, and it starts doing nothing, because nothing's coming in on the standard input. You just see a blank line. So you type on the blank line the string "puts 1+2" and you hit return
When you hit return, the characters on that line are sent to standard input. cat accepts them and sends them to standard output, which at the moment is pointing at calc.rb. (if you skip over to another terminal and change into this directory, you can type "du calc.rb" - the number in the first column reflects that data has been written to this file)
So you've send a line of text to the file, and that's all you want to send, so now you send terminator, to get back to the command prompt. The EOF marker is the CTRL-D character, which you get by holding down the control key and pressing 'd'. If that appears as the first character in a blank line, cat will finish and you'll have text in calc.rb.

You can verify that it worked:

>> cat calc.rb

will again take STDIN and echo it to STDOUT. In this case, STDOUT is just STDOUT, your console, because it hasn't been redirected, but it's taking the filehandle calc.rb as the source for STDIN. So it'll take the contents of that file and write them to STDOUT, which is your terminal.

Should produce

puts 1+2
Was This Post Helpful? 3
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 11:59 AM

Quote

cat > calc.rb puts 1 + 2


is going to try to do something a little different. It's going to take calc.rb as the target for STDOUT, so it's redirecting as you intended. Then it's going to read the argument list and it'll try to use each of those as files, and send their contents one at a time to STDOUT. Since you don't have files called puts, 1, +, or 2, this fails.


Try this: (Assuming you have no files called foo, bar, or baz)

>> cat > foo
foo!
^D
>> cat > bar foo
>> cat bar ## should produce foo!
>> cat > baz foo bar
>> cat baz ## should produce foo! twice, on two lines

I hope this gets you a little clear on the > operator. There's also >>, which means "append to the file if it exists"

>> cat >> baz
!foo
^D
>> cat baz ## same as before with an extra line
Was This Post Helpful? 0
  • +
  • -

#10 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 12:45 PM

So that was very helpful Jon. Thanks. I'm clearly very new at this. So everything is a bit foreign. Thanks for your patience. I put this in

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > calc.rb
puts 1+2
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat calc.rb
puts 1+2

I then opened a new terminal and put in "du calc.rb" as you suggested and it returned

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ du calc.rb
8 calc.rb
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$

What you described worked but how do I get it to output "3" from "puts 1+2"?

Ive also was using sublime text 2 as Text Editor to write the file and then save as "ruby calc.rb." Thanks for you help.

The second portion started like this

"Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > foo foo!
cat: foo!: No such file or directory
Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$

Then tried this

Mark-and-Ashley-Hustads-MacBook-Pro:~ mahustad$ cat > foo which appeared to do what it was supposed to. However, I can't seem to get it output anything after that.

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 12:58 PM

Quote

What you described worked but how do I get it to output "3" from "puts 1+2"?


Once you have the source code (in this case, one line that says "add 1 and 2 and output the result to the user's screen") in a file, you just run it as you were doing before:

>> ruby calc.rb

Quote

mahustad$ cat > foo foo!
cat: foo!: No such file or directory


So you can see that it's trying to read from a file called foo! and send the output to a file called foo, but the first file doesn't exist. If you want "foo!" to be the text that goes to the file, you need to send it on two lines, as here:

Quote

>> cat > foo
foo!


The syntax is a little weird, I admit. Play with it a little. Create a sandbox directory and put a few small text files in it and see what you can do with the > and >> operators. Keep in mind that unix plays rough: if you tell it to do, it'll do, and it's not going to ask you twice. So the > redirect will overwrite a file without warning, which is why it's good to experiment in a sandbox. Likewise, rm removes files without asking permission, and it doesn't have an undo. This is a good thing, but you have to be mindful when you're in the shell.

Edit: once you're comfortable with this one, you might as well look into the input redirect ( < ) and pipe | operators.
The Free Software Foundation has a pretty good introduction to the command line, which of course you can download for free or read on line - browse around on fsf.org, I think it's in the "shop" section.

This post has been edited by jon.kiparsky: 18 June 2013 - 01:02 PM

Was This Post Helpful? 3
  • +
  • -

#12 hustada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 13

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 01:25 PM

Awesome. That worked. You've been very helpful Jon. I appreciate your time. Thanks.

Awesome. That worked. You've been very helpful Jon. I appreciate your time. Thanks.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: puts 1+2 help (not executing)(newb)

Posted 18 June 2013 - 01:52 PM

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1