What does a ||= b
mean?
What is the difference between a symbol and a string?
What is the difference between '
and "
in Ruby?
Can you give me a function that takes a string and tells me true or false if it has any characters in it?
def empty?(s)
s.size == 0
end
Can you give me a function that takes as its input an array of any number of strings and gives as its output an array with the number of characters in each string of the input array?
def count_strings(arr)
arr.map { |str| str.length }
end
Extra credit: Difference between map
and map!
. Other ruby enumerables.
What is the difference between a class and a module?
What are the differences between public, private, and protected methods?
Extra credit: How, in Ruby, can you call a private method without being that instantiated class?
Can you make a Ruby class that represents a bank account, with methods to withdraw and deposit money?
class BankAccount
def initialize
@balance = 0
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount
end
end
Extra credit: How can you, in one line, create a method to write and read to the balance? (attr_accessor :account
)
How would you test Ruby code?
Wouldn't instance methods be required for the last question so that new bankaccount objects could call the deposit, and withdraw methods?
Joe = BankAccount.new
Joe.deposit(400.00)