Ruby Quiz 15/7/2021 Flashcards
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.
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
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
what is the user of this operator?
!~
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
What is the use of the partition method in a string?
partition(sep) → [head, sep, tail]
“hello”.partition(“l”) #=> [“he”, “l”, “lo”]
“hello”.partition(“x”) #=> [“hello”, “”, “”]
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
[]
“***”
[:@greeting, :@name]
In this example, the instance variables are bound to a specific object and are not bound to self by default.
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
‘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.
What does the following code print? Explain.
class RubyGuide class << self p self p self == RubyGuide.singleton_class end end
# true
What is the output of this code?
require ‘date’
(4.days + 5.weeks).from_now
Error
You need active support in rails to do that
How to print this output?
30 29 28 27 26 25
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.
Which of the following methods can be used in Ruby to get a random number?
rnd
Math.GetRandomNumber
$random
rand
rand(1..10)
Difference between “require” and “load” in module?
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