Ruby objects Flashcards

To know everything about Ruby objects

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

All values are objects. T/F?

A

true

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

What class do all Ruby objects inherit from?

A

Object

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

What module does the object class mix in?

A

The Kernel module

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

What does mixing in the Kernel module do to the built-in kernel functions?

A

it makes them globally accessible

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

In Ruby, objects are directly manipulated. T/F?

A

False. References to objects are manipulated, not the objects themselves.

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

Are method arguments pass by value or by reference?

A

by value

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

What values are passed to methods?

A

object references

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

Methods can use the object references to modify the underlying object. T/F?

A

true

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

What types of values are Fixnum and Symbol treated as in a typical Ruby implementation?

A

immediate values

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

What method is most often used to explicitly create an object?

A

new

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

What does the following code do?

myObject = myClass.new

A

It creates a new object called myObject of type myClass.

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

What class does the method new belong to?

A

Class

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

What method method is invoked to initialize a new object?

A

initialize

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

What are factory methods?

A

They are methods that allows you to initialize an object in different ways.

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

What does the class method do?

A

It returns an object representing the class.

Example:
o = “test” # This is a value
o.class # Returns an object representing the String class

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

What does the following code return?

a = 3.2
puts a.class.superclass

A

Numeric

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

What class is the true root of the class hierarchy in Ruby 1.9?

A

BasicObject

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

What method can be used to determine what class an object belongs to?

A

instance_of?

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

What two methods are used to determine if an object is subclass of a class?

A

is_a?

kind_of?

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

What operator can be used in place of the is_a? method?

A

===

21
Q

Define: object type

A

the set of behaviors that characterize the object or the set of methods it can respond to

22
Q

What method is used to determine if a method can be invoked on an object?

A

respond_to?

23
Q

What class enabled reading from and writing to string objects as if they were IO objects?

A

StringIO

24
Q

What does the equal? method test?

A

Whether two values refer to exactly the same object.

25
Q

What operator is used to allow distinct object instances to be tested for equality?

A

==

26
Q

How does the eql? method different from the == operator?

A

The eql? method does not type conversion on the operands.

27
Q

What is the case equality operator, ===, used for?

A

It tests whether the target value of a case statement matches any of the when clauses of that statement.

28
Q

What does the following expression return?

(1..10) === 5

A

true because 5 is in the range 1..10

29
Q

What does the following expression return?

/\d+/ === “123”

A

true because the string matches the regular expression

30
Q

What does the following expression return?

String === “s”

A

true because “s” is an instance of the class String

31
Q

What does the following expression return?

:s === “s”

A

true because the righthand operator is the same symbol as the left

32
Q

What does the following expression return?

“bug” <=> “string”

A

-1 because “bug” is less than “string” (the letter “b” comes before the letter “s” in the alphabet)

33
Q

What does the following expression return?

1.between?(0,10)

A

true because 0 <= 1 <= 10

34
Q

What mixin defines the between? method?

A

Comparable

35
Q

What mixin defines equality operators in terms of <=>?

A

Comparable

36
Q

What method is used to return a String representation of an object?

A

to_s

37
Q

What method is used to return an Integer representation of an object?

A

to_i

38
Q

What method is used to return an Float representation of an object?

A

to_f

39
Q

What method is used to return an Array representation of an object?

A

of_a

40
Q

What method is an alternative to the to_s method and is useful for debugging?

A

to_s

41
Q

What are the two methods used for copying objects?

A

clone and dup

42
Q

What method can be used to create a deep copy of an object?

A

initialize_copy

43
Q

The clone method copies the frozen state while dup does not. T/F?

A

true

44
Q

the clone method copies and singleton methods of the object while dup does not. T/F?

A

true

45
Q

What does the Marshal.dump class method do?

A

It saves the state of the object.

46
Q

What method can be used to mark an object as untrusted user input?

A

taint

47
Q

What method can be used to test if an object is tainted?

A

tainted?

48
Q

What method is used to unmark an object as tainted?

A

untaint