Ruby objects Flashcards
To know everything about Ruby objects
All values are objects. T/F?
true
What class do all Ruby objects inherit from?
Object
What module does the object class mix in?
The Kernel module
What does mixing in the Kernel module do to the built-in kernel functions?
it makes them globally accessible
In Ruby, objects are directly manipulated. T/F?
False. References to objects are manipulated, not the objects themselves.
Are method arguments pass by value or by reference?
by value
What values are passed to methods?
object references
Methods can use the object references to modify the underlying object. T/F?
true
What types of values are Fixnum and Symbol treated as in a typical Ruby implementation?
immediate values
What method is most often used to explicitly create an object?
new
What does the following code do?
myObject = myClass.new
It creates a new object called myObject of type myClass.
What class does the method new belong to?
Class
What method method is invoked to initialize a new object?
initialize
What are factory methods?
They are methods that allows you to initialize an object in different ways.
What does the class method do?
It returns an object representing the class.
Example:
o = “test” # This is a value
o.class # Returns an object representing the String class
What does the following code return?
a = 3.2
puts a.class.superclass
Numeric
What class is the true root of the class hierarchy in Ruby 1.9?
BasicObject
What method can be used to determine what class an object belongs to?
instance_of?
What two methods are used to determine if an object is subclass of a class?
is_a?
kind_of?
What operator can be used in place of the is_a? method?
===
Define: object type
the set of behaviors that characterize the object or the set of methods it can respond to
What method is used to determine if a method can be invoked on an object?
respond_to?
What class enabled reading from and writing to string objects as if they were IO objects?
StringIO
What does the equal? method test?
Whether two values refer to exactly the same object.
What operator is used to allow distinct object instances to be tested for equality?
==
How does the eql? method different from the == operator?
The eql? method does not type conversion on the operands.
What is the case equality operator, ===, used for?
It tests whether the target value of a case statement matches any of the when clauses of that statement.
What does the following expression return?
(1..10) === 5
true because 5 is in the range 1..10
What does the following expression return?
/\d+/ === “123”
true because the string matches the regular expression
What does the following expression return?
String === “s”
true because “s” is an instance of the class String
What does the following expression return?
:s === “s”
true because the righthand operator is the same symbol as the left
What does the following expression return?
“bug” <=> “string”
-1 because “bug” is less than “string” (the letter “b” comes before the letter “s” in the alphabet)
What does the following expression return?
1.between?(0,10)
true because 0 <= 1 <= 10
What mixin defines the between? method?
Comparable
What mixin defines equality operators in terms of <=>?
Comparable
What method is used to return a String representation of an object?
to_s
What method is used to return an Integer representation of an object?
to_i
What method is used to return an Float representation of an object?
to_f
What method is used to return an Array representation of an object?
of_a
What method is an alternative to the to_s method and is useful for debugging?
to_s
What are the two methods used for copying objects?
clone and dup
What method can be used to create a deep copy of an object?
initialize_copy
The clone method copies the frozen state while dup does not. T/F?
true
the clone method copies and singleton methods of the object while dup does not. T/F?
true
What does the Marshal.dump class method do?
It saves the state of the object.
What method can be used to mark an object as untrusted user input?
taint
What method can be used to test if an object is tainted?
tainted?
What method is used to unmark an object as tainted?
untaint