Ruby Quiz December 2021 Flashcards
Express this code using normal array method.
p [“tommy”, “chuckie”].map(&:upcase)
[“tommy”, “chuckie”].map { |name| name.upcase }
[“TOMMY”, “CHUCKIE”]
module Sample def self.sidekick 'robin' end end
p Sample.sidekick
Another syntax to call this method?
module Sample def self.sidekick 'robin' end end
p Sample::sidekick
Singleton methods of a module can either be called with dot notation (Batman.sidekick) or :: notation (Batman::sidekick).
How to convert “to_boolean” of any value?
s = !! “hello” - double bang
s.class =>TrueClass
Which one is correct?
def is_odd(x) x % 2 == 0 ? false : true end
def is_odd(x)
x % 2 != 0
end
Both are correct
What are called First-class citizens in the Ruby programming language?
procs can be stored in data structures, passed around as arguments, and returned by methods, thus meeting the definition of a “first-class” language component.
What does the following code print?
class Array def say_hi "HEY!" end end
p [1, “bob”].say_hi
"HEY!" The Array class was reopened and the say_hi() method was added for all instances of the Array class.
What does the following code print? Explain the result.
class Computer def initialize @sound = "beep beep" end
def self.about
“Sometimes I go #{@sound}”
end
end
p Computer.about
“Sometimes I go “
The about() method is a class method and does not have access to the @sound instance variable. @sound is only available to the instance methods, not the class methods. Undefined instance variables have a default value of nil.
What does the following code print?
class Xyz def paper unassigned_local_variable end end
What would be the output?
xyz = Xyz.new
p xyz.paper
This raises an exception and provides the following message: undefined local variable or method `unassigned_local_variable’.
What would be the output of the following?
true. class
false. class
nil. class
true. class # TrueClass
false. class # FalseClass
nil. class # NilClass
Do you know fixed object_id in Ruby? if so what would be the output of this?
false. object_id
true. object_id
nil. object_id
false. object_id # 0
true. object_id # 20
nil. object_id # 8
How you will get the current call stack in Ruby?
def foo bar end def bar puts caller end foo
What this line will do?
ruby -n -e ‘p eval($_)’
This works because the -n flag does this:
-n assume ‘while gets(); … end’ loop around your script
And $_ is a global variable. Which contains the following:
The last input line of string by gets or readline.
It will act as mini IRB
- object_id => 3
- object_id=>5
- object_id=>7
How these ids are generated?
Integer ids use this formula: (number * 2) + 1.
queue = []
%w{hello x world}.each do |word|
queue «_space;word and puts “Added to queue” unless word.length < 2
end
puts queue.inspect
is this valid? If so, what is the output?
# Output: # Added to queue # Added to queue # ["hello", "world"]
we can use use ‘and’ and ‘or’ to group operations for single liners
How to see the whole Traceback of an exception in Ruby?
def do_division_by_zero; 5 / 0; end begin do_division_by_zero rescue => exception puts exception.backtrace end
backtrace method