Quiz - June Flashcards
What does the following code return?
book = ["intelligent investor"] great_book = ["intelligent investor"]
book.object_id == great_book.object_id
false
The book and great_book arrays have the same content, but are different instances of the Array class and therefore have different object_ids. Array equality comparisons in Ruby check if arrays have the same content in the same order, not if the arrays are the same exact object.
What is the name of this operator? How it will work?
===
subsumption operator.
“If a described a set, would b be a member of that set?”
(1. .5) === 3 # => true
(1. .5) === 6 # => false
Integer === 42 # => true
Integer === ‘fourtytwo’ # => false
True or False?
> count ||= 0
count ||= 0 gives count the value 0 if count is nil or false.
True
a ||= b
The assignment statement supports a set of shortcuts: a op= b is the same
as a = a op b. This works for most operators:
count += 1 # same as count = count + 1
price *= discount # price = price * discount
count ||= 0 # count = count || 0
So, count ||= 0 gives count the value 0 if count is nil or false.
string = “Ruby” + “ 3”
string = “Ruby” “3”
string = “Ruby” «_space;” 3.”
string = “Ruby”.concat(“3”)
puts string
“Ruby 3”
Which one is false?
All are true
there are 4 ways to do string concatenation
What would be the output? class Monster private def method_missing(name, *args) puts "blah monster, attack!" end end
ob = Monster.new
ob. method :output
ob. method :name
ob. name
ob. output
ob.name alone will give output. rest of the ob.method will raise errors.
What would be the output?
class Mammal def self.about "we are living creatures" end end
class Dog < Mammal; end
p Dog.about
“we are living creatures”
Class methods are also inherited.
What are differences in these two class definitions?
#Class Definition #1 class Cat < Object end
#Class Definition #2 class Cat end
There is no difference between Class Definition #1 and Class Definition #2. Class Definition #1 explicitly inherits from Object, but Class Definition #2 inherits from Object by default (implicitly), so these class definitions are identical.
What does the following code print? Explain.
module X; end module Y; end class Z include Y include X end
p Z.ancestors
[Z, X, Y, Object, Kernel, BasicObject]
When Y is included, it is added immediately to the right of Z in the ancestors chain. When X is included, it is added immediately to the right of Z in the ancestors chain, thus moving Y one position to the right.
Is this Valid code?
module Batman def self.sidekick 'robin' end end
p Batman::sidekick
‘robin’
Singleton methods of a module can either be called with dot notation (Batman.sidekick) or :: notation (Batman::sidekick).
Is this valid?
class Dog; end dog = Dog.new def dog.bark "ruff ruff" end
p dog.bark
“ruff ruff”
dog is an instance of the Dog class and is given a singleton method bark(). The bark() method can be called on the dog instance of the Dog class, but is not available for other instances of the Dog class.
What is the difference between to_s and to_str methods?
Do you see the difference? In the first case,
we cast an object into a string, so we call the to_s method.
In the second example, we don’t cast object into a string.
We want to object behave like a string.
We want to object allow to use the :+ method the same as strings do
What is the difference between Explicitly casting and implicitly coercing types in Ruby?
The most common casting helpers are #to_s, #to_i, #to_a and #to_h. These are explicit casting methods. They help us easily transform a value from one type to another.
Ruby also offers implicit coercion methods which only return a value when objects act like the type. This way we can be sure that the value acts like the type we want. These implicit coercion methods are #to_str, #to_int, #to_ary and #to_hash
What is the use of Undef Keyword?
Ruby provides a special keyword which is known as undef keyword. This keyword used to avoid the current working class from responding to calls to the specified named methods or variable.
Which one is not a reserved keyword in Ruby?
a) defined?
b) alias
c) _ FILE _
d) nil
All are reserved keywords
If we do dynamic modification to a class (means to add new or overwrite existing methods) at runtime that technique will be called as ?
In Ruby, a Monkey Patch (MP) is referred to as a dynamic modification to a class and by a dynamic modification to a class means to add new or overwrite existing methods at runtime.