learn_ruby_ch2_pt1 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

hello with inheritance (Hello, Goodbye, friendly)

A
class Hello
  def howdy
    greeting = "Hello, Matz!"
    puts greeting
  end
end
class Goodbye lt Hello
  def solong
    farewell = "Goodbye, Matz."
    puts farewell
  end
end

friendly = Goodbye.new

friendly. howdy
friendly. solong

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Check if it is an integer
Check class type
Convert to float
A

x = 100

x. kind_of? Integer # => true
x. class # => Fixnum
x. to_f # => 100.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
instance variable prefix
class variable prefix
global variable prefix
Constant
A

@hello = hello
instance variable is a variable that is referenced via an instance of a class

@@times = 0
class variable is shared among all instances of a class

$amount = “0.00” # global

Matz = “Yukihiro Matsumoto” # constant starts with Capital or all upper case

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

Parallel assignment

A

x, y, z = 100, 200, 500

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

String assign (thoreau)
Range from start
Range from end

A

thoreau = “If a man does not keep pace with his companions, perhaps it is because
he hears a different drummer.”

thoreau[37..46] # => “companions”

thoreau[-8..-2] # => “drummer”

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

For each character from thoreau string

A

thoreau.each_byte do |c|
print c.chr, “/”
end
# => I/f/ /a/ /m/a/n/ /d/o/e/s/ /n/o/t/ /k/e/e/p/ /p/a/c/e/ /w/i/t/h/ /h/i/s/
/c/o/m/p/a/n/i/o/n/s/,/ /p/e/r/h/a/p/s/ /i/t/ /i/s/ /b/e/c/a/u/s/e/ /h/e/ /h/e/a/r/s/
/a/ /d/i/f/f/e/r/e/n/t/ /d/r/u/m/m/e/r/./

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