Skip to content

Latest commit

 

History

History
274 lines (206 loc) · 4.48 KB

meta.textile

File metadata and controls

274 lines (206 loc) · 4.48 KB

Answer 5/6

If a module has already been included by the current entity or by one of the ancestors of the current entity then the second statement to include module is ignored.

##
module_eval accepts both proc and string. True or False.

###
p = Proc.new do
def hello
puts ‘hello’
end
end

String.module_eval &p
Hash.module_eval &p

Note that module_eval acceps both proc and string. Above case could also be written as

p = %Q{
def hello
puts ‘hello’
end
}

String.module_eval p
Hash.module_eval p

##
Module.new accepts both string and proc. True or false.

###
In the above case method hello was added from outside the class. What if you are inside the class and you want to add a method called hello.

class String
include Module.new {
def hello
puts ‘hello’
end
}
end

Note that Module.new only accepts block. Unlike module_eval it does not accept string. So if you have string then you can do something like this inside the class.

class String
p = %{ def hello; puts ‘hello’; end }
self.module_eval p
end

##
module.constants returns all the constnats defined in a module. The list includes all the constants defined in the included module. True or False.

###
True

##
Unbound method works for both Class and Module. True or False.

##
Unbound method retains its value even after original method has been altered. True or False.

###
module Lab
def foo
puts ‘foo’
end
end
puts Lab.instance_method(:foo).class

class Hello
def foo
puts ‘foo’
end
end
puts Hello.instance_method(:foo).class

Also Remember that unbound method values are preserverd forever. After having the unbound method the original method can be changed and it will have no impact on unbound method.
Look at an example here http://ruby-doc.org/core/classes/UnboundMethod.html

##
What is the name of method which returns the list and order in which a method is searched through all the way to Object.

###
module.ancestors rerturns all the module included in the module ( including self ). Don’t be fooled by the name. This method has nothing to do with ancestors.

##
What will be the output in the following case.

def proc_return
Proc.new { return “Proc.new”}.call
return “proc_return method finished”
end

def lambda_return
lambda { return “lambda” }.call
return “lambda_return method finished”
end

puts proc_return
puts lambda_return

###
First case => Proc.new
Second case => lambda_return method finished

http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
lambdas have diminutive returns. What this means is that while a Proc return will stop a method and return the value provided, lambdas will return their value to the method and let the method continue on.

##
Name another method which behaves close to UnboundMethod.

###
class Array
def iterate!(code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end

def square(n)
n ** 2
end
m = method(:square)

def square(n)
n ** 3
end

array = [1, 2, 3, 4]
array.iterate!(m)
puts array.inspect

##
module_eval is an alias to instance_eval. True or False

###
?

##
instance_eval creates an instance method. True or False.
class User
end
User.instance_eval do
def skill
‘programming’
end
end

###
Above method creates a class method on Person.

##
class_eval creates an instance_method. True or False.
class User
end
User.class_eval do
def skill
‘programming’
end
end

###
Ironically class_eval creates an instance method and instance_eval creates a class method.

##
What’s the output in this case.
class Person
end

p = Person.new

puts p.ancestors

###
undefined method ancestors for p

##
What is the output in this case
class Person
def self.hello
‘hi’
end
end

class << Person
def hello
‘hey’
end
end

puts Person.hello

###
hey . Because metaclass is accessed first.

  1. What is the output in this case. Notice that there is super in second hello method.

class Person
def self.hello
‘hi’
end
end

class << Person
def hello
‘hey’
super
end
end

###
no super class method hello

##
class Person
def hello
‘hi’
end
end

Person.class_eval do
include Module.new {
def hello
‘hello’
end
}
end

puts Person.new.hello

###
???

##
class Person
end

Person.instance_eval do
include Module.new {
def hello
‘hello’
end
}
end

puts Person.new.hello

###
??

##
class Person
end

Person.class_eval do
include Module.new {
def hello
‘hello’
end
}
end

puts Person.hello
puts Person.new.hello

###