Ruby Flashcards
Output a string on its own line
puts
Comment out a line
#
Operator to find the remainder when two numbers are divided
%
Format sequence to convert argument to string
%s
Example: puts “Let’s talk about %s.” % name
Format sequence to convert argument as a decimal
%d
example: puts “He’s %d pounds heavy.” % my_weight
Output string on the same line
Escape sequence to create a new line
/n
write a paragraph of text
«s something going on here.
With the PARAGRAPH thing
PARAGRAPH
Print variable to the next line after “puts”
,
Example:
puts “Here are the days: “, days
Escape sequence to create a tab
\t
Escape sequence to make a backslash
\
Take user feedback
gets
Return a String, removing carriage return characters
.chomp()
Load a library
require
String interpolation
{}
Standard input
STDIN
Pass an open file as an argument
File.open(filename)
The name of your script
$0
Put argument inputs from command line into an array
ARGV
Add information to a file
.write(“String”)
Read all the data in a file
.read()
Truncate the file to a specific number of bytes
.truncate()
Create a function
def
Define as many arguments as needed
*args
Find the starting position of a file
f.seek(0, IO::SEEK_SET)
Read a line from a file
f.readline()
Equality operator for does not equal
!=
Combined comparison operator
Returns 1 if first argument is bigger, 0 if they’re the same and -1 if second argument is bigger.
Equality operator for equals
==
create a range from 1 - 5
(0..5)
Run a for loop over an fruit array
for fruit in fruits
puts fruit
end
[This is called a code block]
Have a method return the argument untouched
yield
Make a variable readable or writable from outside a class
attr_accessor :variable
Make a variable readable from outside a class
attr_reader :variable
Make variable writable from outside a class
attr_writer :variable
Add a global variable to class
$var
Add an instance variable to class
@var
Add a class variable
@@var
Make class functions accessible from anywhere
public
Hide class functions from being accessed
private
Scope resolution operator
::
Allow a class to use methods from a module
extend
Give a method or variable another name
alias :newname :oldname
Execute a block of code and return value of last evaluated expression
begin
Command to run a block while the program is starting up
BEGIN{}
Command to run a block after the program has executed
END{}
Test if an expression refers to anything recognizable (literal object, local variable that has been initialized, method name visible from the current scope, etc.).
defined?
In a begin/end block, it’s the command to run a last piece of code before the end
ensure
Bumps an iterator (while/until/etc) without executing whatever may remain of the block
next
unconditional re-execution of a code block, with the same parameter bindings as the current execution.
redo
an exception-handling clause
rescue
Inside a rescue clause, this command causes Ruby to return to the top of the enclosing code and try executing the code again
retry