Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created November 19, 2021 07:02
Show Gist options
  • Save rozer007/22c2b406a4f7ded69aa41726b57efeb1 to your computer and use it in GitHub Desktop.
Save rozer007/22c2b406a4f7ded69aa41726b57efeb1 to your computer and use it in GitHub Desktop.
Enumerable in Ruby
Q1) What are enumerable in ruby?
ans:It is module which give the class the capabilities to use collection method.
Q2) What function is necessary to include in class to include enumerable?
ans: def each function is required to present in the class.
Q3) What is the function to check whether a value is present in the enumerable or not?
ans: We use find() function.
Q4) What are the which have pre defined Enumerable module.
ans: Hash, Array, Range, IO.
Q1) what will the output of this program :
class Enum
include Enumerable
def each
yield "spaghetti"
yield "rotti"
yield "salad"
yield "water"
end
end
menu=Enum.new
puts menu
a) error
b) rotti
salad
water
rotti
water
c) can't determine
d) enum object
ans: d) enum object
Q2) What will the output of this statement refer the previous program :
puts menu.drop(3)
a) spaghetti
rotti
salad
b) spaghetti
c) water
d) none of the above
ans: c) water
Q3) What will the output of this statement refer the previous program :
puts menu.reverse_each{|i| puts i}
a) error
b) print the elements in reverse
c) can't define
d) water
salad
rotti
spaghetti
ans: d)
Q4) What will the output of this statement refer the previous program :
puts menu.maximum()
a) "water"
b) "rotti"
c) error
d) none of the above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment