Ruby Quiz 15/7/2021 Flashcards

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

What is the use of Undef Keyword?

A

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.

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

Which one is not a reserved keyword in Ruby?

a) defined?
b) alias
c) _ FILE _
d) nil

A

All are reserved keywords

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

If we do dynamic modification to a class (means to add new or overwrite existing methods) at runtime that technique will be called as ?

A

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.

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

What would be the output of this code?

h1 = {a: 1, b: 2}
h2 = h1.merge!({lala: “word up”})
puts h1.object_id == h2.object_id

A

true

The Hash#merge! method combines two hashes and mutates the original hash. Since the haha and bozo variables are assigned to the same object, they have the same object id. If the Hash#merge method was used (notice no !), then the original object would not have been mutated and the object ids would be different

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

what is the user of this operator?

!~

A

which is the “NOT match” operator.

if “Ruby Quicktips” !~ /\d+/
puts “The string does not contain any digits.”
end
# The string does not contains any digits.
# => nil

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

What is the use of the partition method in a string?

A

partition(sep) → [head, sep, tail]

“hello”.partition(“l”) #=> [“he”, “l”, “lo”]
“hello”.partition(“x”) #=> [“hello”, “”, “”]

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

What does the following code print? Explain.

h = {}
class Sublime
  @fav = 'caress me down'
  def sing(obj)
    obj.instance_variable_set(:@greeting, 'mucho gusto')
    obj.instance_variable_set(:@name, 'me llamo brad lee')
  end
end
s = Sublime.new
s.sing(h)
p s.instance_variables
p "***"
p h.instance_variables
A

[]
“***”
[:@greeting, :@name]

In this example, the instance variables are bound to a specific object and are not bound to self by default.

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

What does the following code print? Explain.

class Object
  private
  def method_missing(name, *args)
    'This is a terrible idea'
  end
end

p ‘string’
p ‘boo’.fooey
p ‘array’
p [].boggie_down

A

‘string’
‘This is a terrible idea’
‘array’
‘This is a terrible idea’

Object#method_missing is called before the default BasicObject#method_missing can be called. This code would return ‘This is a terrible idea’ for almost all messages sent to objects that inherit from Object cannot respond to. It’s terrible code, but illustrates the method_missing lookup process and how instances of different classes can share the same method_missing.

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

What does the following code print? Explain.

class RubyGuide
  class << self
    p self
    p self == RubyGuide.singleton_class
  end
end
A
#
true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of this code?
require ‘date’
(4.days + 5.weeks).from_now

A

Error

You need active support in rails to do that

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

How to print this output?

30 29 28 27 26 25

A

30.downto(25){ |i| puts i}

The downto() function in Ruby returns all the numbers less than equal to number and greater than equal to limit.

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

Which of the following methods can be used in Ruby to get a random number?

rnd

Math.GetRandomNumber

$random

rand

A

rand(1..10)

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

Difference between “require” and “load” in module?

A

Require reads the file from the file system, parses it, saves to the memory, and runs it in a given place. In require, if you modify the specified file when the script is running, those modifications won’t be applied, Ruby will use the file from memory, not from the file system of the machine.

Load reads and parses files every time the file (in which load is called) is executed

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