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.
.truncate
Empties the file, watch out if you care about the file.
.write(stuff)
write stuff to file
File.open(filename, ‘w’)
instead of opening regularly which is in read for, open in write form
File.exists?
True if it exists, False if it doesn’t
type WHAT.txt
output the text
Functions
They name pieces of code the way variables name strings and numbers.
They take arguments the way your scripts take ARGV.
Using #1 and #2 they let you make your own “mini scripts” or “tiny commands”.
Run, Call, Use a function?
“To ‘run’, ‘call’, or ‘use’ a function all mean the same thing.”
IO::SEEK_SET
IO::SEEK_SET | Seeks to the absolute location given by amount
What’s the difference between %s and %d?
%s expects a string, while %d expects a number
alias
associate a method and make it synonymous with another method.
To alias a method or variable name in Ruby is to create a second name for the method or variable. Aliasing can be used either to provide more expressive options to the programmer using the class, or to help override methods and change the behavior of the class or object.
and
flow control operator with low precedence that means do x and do y. Not to be confused with &&, which is a boolean operator with high precedence.
BEGIN
Designates, via code block, code to be executed unconditionally before sequential execution of the program begins. Sometimes used to simulate forward references to methods (comes from http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-BEGIN,and I’m only slightly sure of what it means).
begin
Together with end, delimits what is commonly called a “begin” block (to distinguish it from the Proc type of code block).
break
terminates execution of a code block and jumps out of it. Think of a case statement, as that’s where I’m most familiar with seeing it being used.
case
INVESTIGATE
class
defines an object and its methods that can be used by ruby
def
used to define a function
defined?
determines if a method refers to something directly (a string, a number, etc.)
do
execute everything that follows as if it were part of the same block
END
defines a section of code that can be run at the end of a code block
end
end of a block of code
ensure
usually run with a rescue statement, even if the rescue statement doesn’t get run, the ensure statement will run
for
keyword to define the start of a loop
in
run the steps in the for loop that match this condition
module
synonymous with function, it’s a scope where local variables are not aware of other variables
not
is not this value
redo
re-executes a code block without regard to conditions of variables or state of the program
rescue
error handling section of code
retry
associated with rescue, has the program try the section of code where the rescue statement resides agan
return
value sent out from a function to the main routine
self
the area of code being rn at any given time
super
INVESTIGATE
then
makes possible a conditional statement on a single line (i.e. without having to use a semi-colon)
undef
dereference a method or class for use in a given scope
yield
called from inside a method body, yields control to the code block (If any) supplied as part of the method call. if no code block has been supplied, calling yield raises an exception.
constants
data type that starts with a capital letter. if value is changed ruby will warn you that you have changed a constant.
Pi = 3.14
string
a collection of character
a = “hello there”
numbers
any digital value
a= 1, b = 24
ranges
a grouping of numbers (1..10) or (1…10)
arrays
a special container that can contain many values under the same variable name value
array = [“one”, 2, “three”, 4]
hashes
similar to an array, but with a way to associate values
hash = {“dog” => “canine”, “cat” => “feline”}
\a
INVESTIGATE
\f
INVESTIGATE
\n
prints a new line
\r
prints a literal carriage return
\t
tab
\v
vertical tab
::
call a constant value
[]
element set, used with arrays
**
exponent arithmetic operator
-(unary)
INVESTIGATE
+(unary)
INVESTIGATE
!
logical not operator
~
binary ones complement operator
«
binary left shift operator
> >
binary right shift operator
combined comparison operator
a bigger than b, 1
equal, 0
b bigger than a, -1
===
special equals option when used within a case statement
!=
not equal
%w
white space separated array
square brackets %w[…], curly braces %w{…} or even something like exclamation marks %w!…!. All of these have the same behavior (returning an array)