Quiz - May Flashcards
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
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.
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”
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.
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
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.
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’)
“metaprogrammingruby”
The name parameter is assigned to the method name (:meta) and args is assigned to an array of the arguments ([‘programming’, ‘ruby’]).
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
[: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.
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
[:@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.
Compare eql? and edual? methods
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 do you remove nil values in array using ruby?
> > [nil,123,nil,”test”]
=> [123, “test”]
> > [nil,123,nil,”test”].compact
=> [123, “test”]
if false
value = ‘test’
end
What will be the value of:
defined? value
defined? none
defined? value #=> "local-variable" defined? bar #=> nil
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
> > Test.new.send(:method)
“I am a private method”
Yes, with the help of the send method.
What does the following code print? Explain.
class Pencil; end
p Pencil.class p Pencil.superclass
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.
What does the following code print? Explain.
p BasicObject.superclass
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.
What does the following code print? Explain.
p BasicObject.singleton_class.superclass
Class BasicObject's singleton class inherits from Class.
versions = {:ruby=>”3”}
What is the difference between the following lines of code?
versions[:rails]
versions.fetch(:rails)
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.
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)
=> [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}