learn_ruby_ch2_pt1 Flashcards
hello with inheritance (Hello, Goodbye, friendly)
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
Check if it is an integer Check class type Convert to float
x = 100
x. kind_of? Integer # => true
x. class # => Fixnum
x. to_f # => 100.0
instance variable prefix class variable prefix global variable prefix Constant
@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
Parallel assignment
x, y, z = 100, 200, 500
String assign (thoreau)
Range from start
Range from end
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”
For each character from thoreau string
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/./