Learn Ruby the Hard Way: Words & Symbols Flashcards
terminal:
irb
into ruby realm!
mkdir NAME
Make Directory
cd NAME
Change Directory
puts
write out string and create new line
ruby ex1.rb
run ex1.rb
Caret character, points out where the error is
#
Anything after this on the same line is a comment
+
plus which can add two things
-
minus which can subtract two things
/
slash which can divide two things
*
asterisk which can multiply two things
%
percent which does the modulo thing and string substitution; example
“We are a %s and %s.” % [animal1, animal2]
<
less than
>
greater than
<=
less-than-equal
> =
greater-than-equal
=
equal, set a variable
_
underscore
format string
“ANYTHINGINHERE”
string
something a human will read or be given
string interpolation
put a defined string within a string using “#{}”
\n
put a new line character into the string at that point.
<<WHATEVER text text WHATEVER
document syntax, which uses «NAME and works like a string, but you also can put as many lines of text you as want until you type NAME again.
\
These two characters will print just one back-slash.
\
Escape technique.
To solve this problem you escape double-quotes and single-quotes so Ruby knows to include in the string. Here’s an example:
“I am 6’2" tall.” # escape double-quote inside string
‘I am 6'2” tall.’ # escape single-quote inside string
\t
tabbed
Notice that we are using print instead of puts to do the prompting. print doesn’t add a new line automatically, so your answer can go on the same line as the question. puts on the other hand, adds a newline automatically.
.gets
takes user input and puts on new line
.chomp
since .gets automatically adds a new line this method will destroy that new line, putting the response on the same line
require “whatever”
his is how you add features to your script from the Ruby feature set or other sources (e.g., Ruby Gems, stuff you wrote yourself). Rather than give you all the features at once, Ruby asks you to say what you plan to use. This keeps your programs small, but it also acts as documentation for other programmers who read your code later.
feature = libraries = modules
feature
= libraries = modules
argument
You know how you type ruby ex13.rb to run the ex13.rb file? Well the ex13.rb part of the command is called an “argument”
What we’ll do now is write a script that also accepts arguments.
Type this program and I’ll explain it in detail:
1 2 3 4 5 6 first, second, third = ARGV
puts “The script is called: #{$0}”
puts “Your first variable is: #{first}”
puts “Your second variable is: #{second}”
puts “Your third variable is: #{third}”
The ARGV is the “argument variable”, a very standard name in programming, that you will find used in many other languages.
ARGV
The ARGV is the “argument variable”, a very standard name in programming, that you will find used in many other languages. It’s in all caps because it’s a constant, meaning you shouldn’t change the value once it’s been assigned. This variable holds the arguments you pass to your Ruby script when you run it.
STDIN
Also notice that we’re using STDIN.gets instead of plain ‘ol gets. That is because if there is stuff in ARGV, the default gets method tries to treat the first one as a file and read from that. To read from the user’s input (i.e., stdin) in such a situation, you have to use it STDIN.gets explicitly.
prompt
puts forward something before the user input, like >
Hard coding
“Hard coding” means putting some bit of information that should come from the user as a string right in our program. That’s bad because we want it to load other files later. The solution is to use ARGV and STDIN.gets to ask the user what file they want instead of “hard coding” the file’s name.
txt = File.open(filename)
puts txt.read()
We call a function on txt. What you got back from open is a file, and it’s also got commands you can give it. You give a file a command by using the . (dot or period), the name of the command, and parameters. Just like with File.open. The difference is that when you say txt.read() you are saying, “Hey txt! Do your read command with no parameters!”
Reads the contents of the file, you can assign the result to a variable.
.close()
close files you open
Closes the file. Like File->Save.. in your editor.
.readline()
Reads just one line of a text file.