Rookie - Ruby: Run file in terminal - ruby

Very inexperienced Ruby student here.
I’m not sure how to write a Ruby program in Atom, save it then try to run it in the terminal (Mac OSX).
Could someone run me through the absolute basics, please?
Forgive my cluelessness!

Ruby programs generally use the '.rb' extension, so in order to run a ruby file that you've written, you need to save it somewhere with that extension first- eg. 'my-app.rb'.
It's a good idea when starting out to save it in a folder inside your "Home" directory (/Users/your user name/). You can find that in the mac "Finder" by clicking on the folder on the left hand list that's named "your username". In your terminal, your home directory is shortened to '~/' - and you can easily change directory into it with that shortcut:
cd ~
While I've been learning, I've stuck to a quick, short directory to store my files- '~/code/'. Anything will do, but it's much quicker to type 'cd ~/code/my-app.rb' than to type something long like 'cd ~/Documents/Programming/Ruby/my-app.rb' every time. So when you're deciding on where to save, think about how much you'll have to type in terminal! :)
Once you've saved your file, and used 'cd' to change into the directory you've saved it in, you use the command 'ruby' to run it.
ruby my-app.rb
That's about all there is to actually running your file! There's so much more to using the terminal, and writing code- but there's plenty of info out there on how to start.
I found Chris Pine's "Learn To Program" really simple and easy to follow. There are plenty of other resources out there, too! Try out Try Ruby to get going straight in your browser.

Related

How do you go to a local directory in PuTTy?

I'm not used to using PuTTy so I'm not very well versed with all the know hows to use it. So after I log onto the HostName/IP address and log in with my username and ID, there's a local directory /u/SysLab/ where one of the file belongs to me and that's where I'm supposed to do my coding, but I'm not sure how to access it, can anyone tell me how to do this? Me googling "How to go to local directory" yields questions that ask how to transfer files to the server, which isn't what I want to do. I just want to go into the directory and start my coding homework. Can anyone help me?
Did you try cd /u/SysLab?
That should change your current directory to /u/SysLab/ after that you should be able to open the file that you want to work on using some editor e.g. if use Emacs then you could do emacs -nw <yourfile>.
I strongly recommend to first get familiar with (at least) some basic navigation and file handling/editing in the terminal before starting to code.
Assuming your OS is Linux/Unix based, cd (change directory) and the path should work for you: cd /u/SysLab

Cannot understand folder structure for ruby project in linux

I am learning rspec and cucumber from the book - The Rspec book. This question is not about those two things. It is more about linux. My book gives me instructions which make no sense. Please help me to understand it.
Create a bin in the project root directory (sibling to lib and spec), and add a bin/codebreaker file. If you’re on a *nix system, enter this code in that file:
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path( '../../lib' , __FILE__)
require 'codebreaker'
game = Codebreaker::Game.new(STDOUT)
game.start
Shouldn't that be a bin/codebreaker folder ? Anyway, I made a folder bin, under project root. I also made a file bin/codebreaker.rb with the above code and continued.
If you’re on *nix, now run chmod +x bin/codebreaker so we can execute
it, and then run this:
$ bin/codebreaker
Welcome to Codebreaker!
Enter guess:
Now look at that! Who knew that this little bit of code was actually going to start to make something work?
I don't get the above output when I go to project root and execute bin/codebreaker from there. I only see this output -
bash: bin/codebreaker: Is a directory
Am I missing something ? How do I make this work ?
The instructions tell you to create a file named codebreaker. However, you created a file named codebreaker.rb, not codebreaker. In addition to the file named codebreaker.rb which you weren't supposed to create, you also created a directory named codereaker which you also weren't supposed to create.
I don't know CodeBreaker, but the instructions you posted should work, although they are a little ugly (e.g. manually fiddling with $LOAD_PATH instead of simply using require_relative). Just follow them. Or if you do deviate from them as you did with naming your file codebreaker.rb instead of codebreaker, then you must of course also adapt all further instructions to that change, e.g. for example running bin/codebreaker.rb instead of bin/codebreaker.

OS X - Make returning "Nothing to be done for <filename>"

First of all, I would like to apologize if I'm on the wrong stackexchange network, and secondly, sorry if I'm overlooking something simple.
I was moving files from my old hard drive from an old PC when I came across several password protected ZIP Files. However, since those files were a bit old, I forgot the passwords already. I tried every password I could come up with but I still came up empty. After several google searches, I found this tool/utility for Mac OS X that could help me. So I go to the downloads page and download the Mac OSX utility tool and the source code.
However, I am having problems executing the make command for the file. It says on the downloads page:
If you are using linux or another unix, you need to download the source code, uncompress it and type "make" to compile the utility.
So far, what I've tried is
$make /Users/myname/Downloads/aapr-0.01-source/Makefile
I have also tried the other files in the source folder but nothing worked. After that, I'm pretty much blank. I tried double clicking the aapr file in the utility download for Mac (which is a Unix Executable) and it opens a new terminal window displaying the commands and such. Also, doing $aapr [options] [filename] only shows me -bash: aapr: command not found. I also updated XCodes command line tools.
Sorry if I'm missing out on something very basic, I don't usually use the terminal on the OS X.
Try running
cd /Users/myname/Downloads/aapr-0.01-source
make
A Makefile contains rules for building files from other files but it is based on paths and contents, etc. which depend on your current location. So generally you need to be in the directory of the project for it to work.
Edit (copied from my comment):
To execute a command from a specific location (that isn't in the normal $PATH) you need to specify a path for it. So something like /bin/bash or ./aapr (where ./ means the current directory).

How do I run a Ruby script created in text editor in the Mac OS Terminal?

I just started reading the Well-Grounded Rubyist, and I am just beginning to use Ruby in my terminal on my Mac.
I'm on the very first lesson, creating a Celsius to Farenheit converter in a text editor. I've saved the code as an .rb file by using Textmate (my text editor). The file name is c2f.rb. The file is saved in a folder on my desktop titled "Rubycode".
I am having difficulty running the .rb file in the terminal however. I've tried many different methods of trying to call the file, including using:
cd /Users/rexrose/Desktop/Rubycode/c2f
and many others.
Any thoughts on what exactly, I'm supposed to type into terminal in order to call the c2f file?
Thanks in advance.
I just started reading Well-Grounded Rubyist.
That's a very good book. I consider it more of an intermediate level book than a beginner book, but no matter.
I've tried many different methods of trying to call the file,
including using
cd /Users/rexrose/Desktop/Rubycode/c2f
The cd command means "change directories" and you cannot change directories to a file. Instead, you have to change directories to the directory containing the file:
$ cd /Users/rexrose/Desktop/Rubycode
Then you can execute your program contained in the file c2f.rb like this:
$ ruby c2f.rb
Here are some Terminal tips:
1) You can use ~ instead of /Users/YourUserName, so you can save some typing by doing this:
$ cd ~/Desktop/Rubycode
Typing '~' instead of '/Users/YourUserName' will become second nature.
2) Using the cd command with no arguments:
$ cd
will take you to your home directory, i.e. /Users/YourUserName
3) You should change your prompt to indicate what directory you are currently in. To do that, create a file called .bash_profile in your home directory(/Users/YourUserName). Check to see if it exists first:
$ cd
$ ls -al
The command ls -al will show all the files in a directory, including hidden files, which are files whose name begins with a .. If a file named .bash_profile exists, open it; if it doesn't exist, create it. Put this in .bash_profile:
PS1="\w$ "
To get Terminal to recognize the changes, you can either Quit Terminal and relaunch it, or do this:
$ source .bash_profile
Then open a new Terminal widow.
You can also add 'aliases' to .bash_profile. For instance, in my .bash_profile I have the alias 'r' for 'ruby', so that I can execute a ruby program like this:
$ r my_program.rb
In .bash_profile you make an alias like this:
alias r="ruby"
4) Tab completion in Terminal:
You might have noticed that you can type part of a file name, then hit tab and Terminal will complete the file name. Using tab completion, I can execute my ruby program like this:
$ r my_pr<tab>
In fact, I name my practice ruby programs so that I can use tab completion to the greatest effect. I have files named 1.rb, 2.rb, 3.rb, and then I execute one of them by simply typing:
$ r 1<tab>
And in fact, you may not even have to type that! If you hit the up arrow key on your keyboard, Terminal will display the previous command, and if you hit the up arrow key again, you will see the command before that. So you can scroll up to a previous command, then hit return to execute it--without having to type anything.
You should endeavor to use tab completion for each of the file names in a path. For example, if you are cd'ing to /Users/YourUserName/dir1/dir2, you should do this:
$ cd /Use<tab>/YourUser<tab>/di<tab>/di<tab>
The reason you should use tab completion for each filename(by the way in Unix filename is a general term for both directory names and file names) is because when the name won't tab complete, then you are in the wrong directory or you are trying a filename that doesn't exist in that directory. So instead of having to type out the whole path '/Users/YourUserName/dir1/dir2' and then finding out about the error when you hit return, the tab completion will let you know immediately when there is an error(because the filename won't tab complete)--saving you some typing.
5) Because you will probably be using Terminal for mostly ruby programs for awhile, you can set up things so that Terminal will automatically open up in your directory Users/rexrose/Desktop/Rubycode. Put this in .bash_profile:
cd "/Users/rexrose/Desktop/Rubycode" (Here you cannot use ~)
6) Occasionally, you may have to type a long file name that exists on your computer into the command line:
$ cd /Library/SomeLongName/AnotherLongName34832o222/142582dir/some_file.txt
Instead of having to type all that at the command line, you can locate the file in Finder first. Then if you drag the file onto the Terminal window, the file name will be entered at the point of the cursor.
Finally, a better way to organize your files might be to put them in directories below your home directory, like this:
~$ mkdir ruby_programs
~$ cd ruby_programs
~/ruby_programs$ mate 1.rb
First things first: cd stands for "Change directory".
Normally the terminal should open in "~", which is the home directory where most of your things are. In OS X it will be /Users/[username]. It's also possible possible that in OS X, it will save the location of the last session. I also recommend, since you're starting to install, "Iterm2", which is a nice terminal to use. It supports multiple tabs, etc.
Ruby, the interpreter, is the command "ruby". To call a script you have to call Ruby with a filename:
ruby /Users/rexrose/Desktop/Rubycode/c2f/c2f.rb
That is almost the equivalent of:
cd /Users/rexrose/Desktop/Rubycode/c2f/
ruby c2f.rb
It's almost equivalent, but for now the difference shouldn't bother you. Let say that the second way to call the script is more favorable than the first.
Now, the second thing: If you want to try things in Ruby, you can start an interactive shell. The command is "irb".
Type irb and Enter and then you can type Ruby code. If you want to leave, press CTRL+C multiple times.
The last thing, I recommend installing "RVM". It will save you time and pain, I hope. If you want to install Ruby gems, it will not mess with the Ruby already present with the system. That's my personal opinion but I believe lots of people will agree. Even if Ruby comes with OS X, you should install a different Ruby for development. It will make sure that if something goes wrong in dev, it will not mess the Ruby OS X might be using.

How to set up system path for Ruby?

I am doing prep work for app academy. The final stage before I am done with my prep work is to complete a ruby intro course called "Test First Ruby".
The first line after you install Rspec is to enter the course directory. In the terminal it is "cd learn_ruby", simple enough, except it returns back a message that says "the system cannot find the path specified". I have been noticing this message on certain commands for all of my ruby learning thus far and I am just wondering what does this mean? And how can I fix this?
Any help would be great.
cd means 'change directory'. The error you are getting is that the directory is not existant in the location your command line is in.
This looks like a reasonable intro to UNIX filesystem: http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture2.html
UNIX reference: http://sunsite.utk.edu/UNIX-help/quickref.html
Create the directory before you cd into it:
mkdir learn_ruby
cd learn_ruby
Do check out AJcodez's links to familiarize yourself with Unix filesystem commands.

Resources