try ruby Flashcards
reverse a string
.reverse
length of a string
.length
print string x times
”” * X
reverse number
1.to_s.reverse
to string
.to_s
to integer
to_i
to array
to_a
declare array / assign array
[] / x=[1,2,3]
[] creates an empty what
list
does a list store thing out of order
false
get max from array
.max
sort array
.sort!
method with ! does what
changes value for good
replace all string x in an array containing x
arr[‘x’]=’y’
reverse an array with a string of lines, (only each individual line) without writing words backwards
poem.lines.to_a.reverse
join elements in an array
.join
check to see if something is included
.include?
lowercase everything
.downcase
delete an obj
.delete
dictionary or hash
books={}
add elements to a dictionary/hash
books[“title”] = :splendid
can you use length on a dictionary/hash?
yes, will return number of items within
iterate through a dictionary/hash’s values
books.values.each{|rate| ratings[rate]}
iterate and count number of times rate appears through a dictionary/hash’s values
books.values.each{|rate| ratings[rate]+=1}
returns like {:good=>2, :bad:=>5}
how do you create a block in ruby
curly braces
a way of performing something x times
5.times {print “hi! “}
how to create a symbol
:symbolhere
blocks
chunk of code which can be tacked oon to many of rubys methods
access directory
Dir.entries “/” (anything listed after a method is called an attatchment, like the / in this example)
Dir[] does what
searches for files with wildcard characters
read a file
File.read()
copy a file to new dir
FileUtils.cp(‘file1’, ‘destination’)
open file
File.open()
using the pipe character in a do statement
File.open(“txt.txt”, “a”) do |f|
f«_space;“Cat”
end
check when a file was changed
File.mtime(filename.txt)
can also append .hour to it to just get hour
how are arguments seperated in ruby
commas
define a method in ruby
def methodname
code
end
split
method for strings, breaking it into an array based on the given char to split on
strip
removes extra whitespace around the url
p line is short for what
paragraph
create a list
list do
p”1”
p “2”
end
to load a library
require ‘library’
the .new method does what
used to make objects of a certain class
define a class
class Nameofclass
How to instantiate a class
x=NAmeofclass.new
what is attr_accessor
attributes or variables attatched to the class, used for acccesing outside the class
how to declare an instance variable
@x
what are instance variables
vars for accesing inside the class
sort attributes with a time association in ascending order
blog.sort_by { |entry| entry.time }.reverse
add new entries
«_space;x
what can the map method do
cycle through an array and replace each item with something new
Time method
Time.now
Time.now-2.weeks
use a variable in a string
“Hello #{variable}”
execute a block of code x times
x.times do
code
end
iterate through an array
array.each do |n|
puts n
end
unless
evaluates to if not, do
an if, else if, else statement in ruby
if x==0 puts x elsif x==1 puts x else puts x end
.empty?
boolean for if something is empty
nil is treated as what in ruby
false
path of a file method
.file_path
empty strings, 0, and empty arrays are treated as what
true
fail raises what kind of error if true
runtimeerror
written fail “wrong password”
will raise exception wrong password