Quiz - May Flashcards

1
Q

What does the following code print?

def add(x, y)
  return(x + y)
end

result = add(4, 5) do
puts “Hey Ruby”
end

p result

A

9

The add() method is passed a code block, but it doesn’t require the code block. When methods are passed a code block but don’t require the code block, the code block is simply ignored.

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

is this valid code?

class Learning
  def i_learn
    x = 'ruby'
    y = 'rails'
    z = 'sqlite3'

end
end

b = Learning.new.i_learn
p eval('"The #{self.class}, the #{x}, the #{y}, the #{z}"', b)

How can we get this output?
output
“The Learning, the ruby, the rails, the sqlite3”

A

Binding objects can be used as the second argument of eval() to set the scope. The string of code passed as the first argument in eval() is evaluated in the context of the binding passed in the second argument.

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

Add code to the following class so

Motivation.new.speak returns “Go speed racer!!!”

first = 'speed'
second = 'racer'
class Motivation
  def speak
    # add code here
  end
end
A

The TOPLEVEL_BINDING constant refers to the top level object. Note that this code will not work if it’s pasted into the IRB console, this code must be run from a file with the normal Ruby interpreter.

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

What does the following code print? Explain.

class A
  private
  def method_missing(name, *args)
    args.unshift(name.to_s).join
  end
end

p A.new.meta(‘programming’, ‘ruby’)

A

“metaprogrammingruby”

The name parameter is assigned to the method name (:meta) and args is assigned to an array of the arguments ([‘programming’, ‘ruby’]).

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

What does the following code print? Explain.

class Human
  def Human.about; end
  def self.generation; end
  def hi; end
  instance_eval do
    def bye; end
  end
end

p Human.singleton_methods

A

[:about, :generation, :bye]

There are several ways to define singleton methods. Notice that the Human#hi method is a regular instance method and is not a singleton method.

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

What does the following code print? Explain.

class Soccer
  @fun = 'woo hoo'
  def initialize
    @goal = 'score'
  end
end

s = Soccer.new
p s.instance_variables

A

[:@goal]

The @goal instance variable is bound to instances of the Soccer class. The @fun instance variable is bound to the Soccer class object itself, not instances of the Soccer class.

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

Compare eql? and edual? methods

A

eql? – Checks if the value and type of two operands are the same (as opposed to the == operator which compares values but ignores types). For example, 1 == 1.0 evaluates to true, whereas 1.eql?(1.0) evaluates to false.

equal? – Compares the identity of two objects; i.e., returns true if both operands have the same object id (i.e., if they both refer to the same object). Note that this will return false when comparing two identical copies of the same object.

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

How do you remove nil values in array using ruby?

> > [nil,123,nil,”test”]
=> [123, “test”]

A

> > [nil,123,nil,”test”].compact

=> [123, “test”]

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

if false
value = ‘test’
end

What will be the value of:

defined? value
defined? none

A
defined? value
#=> "local-variable"
defined? bar
#=> nil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you call a private method outside a Ruby class using its object?

Given the class Test:

class Test
   private
       def method
         p "I am a private method"
      end
end
A

> > Test.new.send(:method)
“I am a private method”

Yes, with the help of the send method.

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

What does the following code print? Explain.

class Pencil; end

p Pencil.class
p Pencil.superclass
A

Class
Object

All class objects (objects that can be used to instantiate other objects) are instances of the Class class. Hash.class, Array.class, String.class, and Pencil.class all return Class.

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

What does the following code print? Explain.

p BasicObject.superclass

A
nil
Every class object (i.e. instance of the Class class) has a superclass, except for BasicObject. BasicObject is at the top of the class hierarchy and does not have a superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the following code print? Explain.

p BasicObject.singleton_class.superclass

A
Class
BasicObject's singleton class inherits from Class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

versions = {:ruby=>”3”}

What is the difference between the following lines of code?

versions[:rails]
versions.fetch(:rails)

A

versions.fetch(:rails) raises an exception because the :railskey is not present in the versionshash (if a second argument was added to the fetch() method, the default value would be returned instead of raising an exception).

versions[:rails] returns nil because the :rails key is not present in the versions hash.

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

What is the output of this code?

def dubSplat(a, *b, **c)
p b
p c
end

dubSplat(1,2,3, 4, a: 40, b: 50)

A

=> [2, 3, 4]
You can also use the splat operator(*) to grab any segment of an array

The double splat operator (**) can be used for hashes!
=> {:a=>40, :b=>50}

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

What is Spaceship Operator in Ruby? How it is working?

A
a <=> b :=
  if a < b then return -1
  if a = b then return  0
  if a > b then return  1
  if a and b are not comparable then return nil
17
Q

What is the name of this operator? How it will work?

===

A

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

18
Q

True or False?

> count ||= 0

count ||= 0 gives count the value 0 if count is nil or false.

A

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.

19
Q

string = “Ruby” + “ 3”

string = “Ruby” “3”

string = “Ruby” &laquo_space;” 3.”

string = “Ruby”.concat(“3”)

puts string

“Ruby 3”

Which one is false?

A

All are true

there are 4 ways to do string concatenation

20
Q

What does the following code return?

book = ["intelligent investor"]
great_book = ["intelligent investor"]

book.object_id == great_book.object_id

A

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.