ruby Flashcards
attr_accessorattr_readerattr_writer
A = B + C
What’s ARGV short for?
Argument vector, 紧跟在ex01.rb后面的参数比如,ARGV.first就是第一个参数
Difference puts/print?
- puts print new line2. puts ignore nil when displaying arrays
Difference: gets, STDIN.gets, STDIN.gets.chomp()
STDIN.gets.chomp() reads user input first;gets.chomp() reads ARGV first;.chomp() removes the /n at the end of the string.
default name of script?
$0
why close files?
to save some memory, or it’ll have to wait until garbage collection, which could come at who knows when
how to read/write a file?
File.open(from_file).read()File.open(to_file, “w”).write(content)
how to take more than one parameter?
use asterisk:def a_function (*args) print args # an arrayend
can we do maths inside of the parenthesis?
yes
how to reset read progress (rewind)?how to readlines?
input_file.seek(0, IO::SEEK_SET)input_file.readline()
a, b, c = [1, 2, 3, 4]
a => 1b => 2c => 3
how to require modules in irb?module Ex25 def self.break_words(stuff) words = stuff.split(‘ ‘) words endend
require ‘./ex25’Ex25.break_words(sentence)
how to generate [1, 2, 3, 4, 5]?
Array(1..5)(1..5).to_a
Range:(-1..-3).to_a(-3..-1).to_a(‘a’..’c’).to_a(‘a’…‘c’).to_a
[][-3, -2, -1][‘a’, ‘b’, ‘c’][‘a’, ‘b’]
how to cycle through an array?
a.cycle { |x| puts x } # goes on forevera.cycle(2) { |x| puts x } # execute twice
how to print the index/item of an array?
a.each_index { |x| puts x }a.each { |x| puts x }
first and last item of an array?
array.firstarray.last, array[-1]
take a part of an array (index 3 to 5), and join them into a string separated by “#”?
array.values_at(3, 5).join(“#”)
the opposite of if?
unless
the opposite of while?
until
print “Odelay!” 5 times
5.times { print “Odelay!” }
what’s the difference between symbols and strings?
you want to use strings for data, for things that you might want to truncate, turn to uppercase, or concatenate. Use symbols when you simply want an intelligible thing that stands for something in your code.e.g.person = {}person[:name] = ‘russ’puts “Name: #{person[:name]}”notputs “Name: #{person[‘name’]}”
difference: $x, @y @@z
Variables which begin with a dollar sign are global.Variables which begin with an at symbol are instance variables.Variables which begin with double at symbols are class variables. Think of the double at prefix as meaning attribute all. Additionally, you can think of a swarm of AT-ATs from Star Wars, which are all commanded by Ruby. You change a class variable and not just one changes, they all change.
Regular expression!
A regular expression (or regexp) is a set of characters surrounded by slashes./ruby/, /[0-9]+/ and /^\d{3}-\d{3}-\d{4}/ are examples.Regular expressions are used to find words or patterns in text. The slashes on each side of the expression are pins.Imagine if you had a little word with pins on both side and you held it over a book. You pass the word over the book and when it gets near a matching word, it starts blinking. You pin the regular expression onto the book, right over the match and it glows with the letters of the matching word.Oh, and when you poke the pins into the book, the paper sneezes, reg-exp!Regular expressions are much faster than passing your hand over pages of a book. Ruby can use a regular expression to search volumes of books very quickly.