ruby Flashcards

1
Q

attr_accessorattr_readerattr_writer

A

A = B + C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s ARGV short for?

A

Argument vector, 紧跟在ex01.rb后面的参数比如,ARGV.first就是第一个参数

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference puts/print?

A
  1. puts print new line2. puts ignore nil when displaying arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Difference: gets, STDIN.gets, STDIN.gets.chomp()

A

STDIN.gets.chomp() reads user input first;gets.chomp() reads ARGV first;.chomp() removes the /n at the end of the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

default name of script?

A

$0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

why close files?

A

to save some memory, or it’ll have to wait until garbage collection, which could come at who knows when

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to read/write a file?

A

File.open(from_file).read()File.open(to_file, “w”).write(content)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to take more than one parameter?

A

use asterisk:def a_function (*args) print args # an arrayend

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

can we do maths inside of the parenthesis?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to reset read progress (rewind)?how to readlines?

A

input_file.seek(0, IO::SEEK_SET)input_file.readline()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

a, b, c = [1, 2, 3, 4]

A

a => 1b => 2c => 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to require modules in irb?module Ex25 def self.break_words(stuff) words = stuff.split(‘ ‘) words endend

A

require ‘./ex25’Ex25.break_words(sentence)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to generate [1, 2, 3, 4, 5]?

A

Array(1..5)(1..5).to_a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Range:(-1..-3).to_a(-3..-1).to_a(‘a’..’c’).to_a(‘a’…‘c’).to_a

A

[][-3, -2, -1][‘a’, ‘b’, ‘c’][‘a’, ‘b’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to cycle through an array?

A

a.cycle { |x| puts x } # goes on forevera.cycle(2) { |x| puts x } # execute twice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to print the index/item of an array?

A

a.each_index { |x| puts x }a.each { |x| puts x }

17
Q

first and last item of an array?

A

array.firstarray.last, array[-1]

18
Q

take a part of an array (index 3 to 5), and join them into a string separated by “#”?

A

array.values_at(3, 5).join(“#”)

19
Q

the opposite of if?

A

unless

20
Q

the opposite of while?

A

until

21
Q

print “Odelay!” 5 times

A

5.times { print “Odelay!” }

22
Q

what’s the difference between symbols and strings?

A

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’]}”

23
Q

difference: $x, @y @@z

A

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.

24
Q

Regular expression!

A

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.