Review Ruby Flashcards
puts “put s”
- ‘prints’ the following on the screen
#
- hash, pound, octothorpe
- starts a comment line or used to disable a line of code
“…”
‘…’
- a string of text
- single quotes do things different. Can be used around a variable in a string, printing the output with single quotes showing
\+ - / * % < ><= >=
PEDMAS
PE(M&D) (A&S)
-plus
- minus
- slash (divide)
- asterisk (multiply)
- percent
(modulus - x divided by y with j remaining) The result of % is the j part , the remainder
{…}
- this is the format for inserting Ruby computations inside text strings. The result of the computation in the ‘printed’ string
Xxx_yyy
Underscore - puts an imaginary space between words in variable names
ex: carpool_capacity
4 vs 4.0
Integer vs floating point number
= (…)
= equals is used to give data (numbers, strings, etc. ) names
-sets a variable
==
- double equal tests whether two things have the same value
x=100 vs x = 100?
- it is bad form to leave out the spaces
variable
- any line of code where you set a name = (equal) to a value
ex. type_of_fish = 10
print vs puts
puts has a linefeed in it
Print does not
{} or %{} ?
Always use #{} to format strings
%{} is used when you want to apply strings multiple times
\n
%q{ - - - }
- escape sequence … linefeed
Print a multi line string
“””
- triple quotes works like a string, but you put as many lines of text as you want until you type
“”” again
note: use ‘’’ when you need a multiline string that contains #{} formatting, but you don’t want them to be processed yet or at all. Use “”” For all other multiline strings.
gets.chomp
gets retrieves a line of text input from the user AND includes a trailing linefeed
chomp suppresses the linefeed
gets.chomp.to_i
Get a string from the user
chomp off the \n and convert too an integer
ARGV
ARGV.first. vs ARGV first 2nd 3rd etc.
Argument variable
- must pass x command line arguments
ex. Ruby ex13.rb 1st 2nd 3rd etc.
Note: ARGV gets input from the command line while gets.chomp input comes in via the keyboard while the script is running.
ARGV.first is for only one argument
$stdin
Standard input is stream data
close
read
readline
truncate
write(‘stuff’)
seek(0)
- closes the file
- reads the contents of the file. You can send the results to a variable.
- reads just one line of a text file.
- empties the file. Watch out if you care about the file.
- writes “stuff“ to the file.
Move the read/write location to the beginning of the file.
Modifiers
w
r
a
w+
- write
- read
- append
- will open file in read and write mode
echo
cat
That;depends;on;how…
- ?
- concatenate, an old command that puts files together. Mostly it’s just an easy way to print a file to the screen.
You make several lines of into fewer by using ;
functions
def
- I name pieces of cove the way variables name strings in numbers.
- They take arguments the way scripts take ARGV.
- Using one and two, they let you make your own mini scripts or tiny commands