-1

There is a method called all? in Enumerable.

I'm trying to learn all the methods of Enumberable's library by writing them myself.

This is what I've come up so far for the all? method. I sorta understand it but I got stumped when trying to pass initialized values to my method.

EDIT for the record, I'm aware that enum method that I have is not the right way ie, it's hard-coded array. This is for self-learning purposes. I'm just trying to figure out how to pass the initialized values to my all? method. That's why I wrote enum in the first place, to see that it is working for sure. Please don't take this class as a literal gospel. Thank you.

class LearningMethods

  def initialize(values)
    @values = values
  end

  def enum
    array = [10, 3, 5]
  end


  def all?(a)
    yield(a)
  end

end

c = LearningMethods.new([10, 3, 5])
p c.enum.all? {|x| x >= 3 } #this works

p c.all?(10) { |x| x >= 3 } #this works

p c.all?(@values) { |x| x >= 3 } #this doesn't work. Why not? And how do I pass the initialized values? 
user273072545345
  • 1,536
  • 2
  • 27
  • 57
  • 2
    Please define what you mean by, *This doesn't work*. Various things could be wrong here. From the code you show, `@values` isn't defined in the scope you're using it on the failure line. – lurker Oct 02 '14 at 20:59
  • 2
    There are too many problems with your code. I'm not sure that it would even be helpful for me to point them out. You should read up on basic uses of classes, blocks, and yield before trying to reimplement Enumerable. – Max Oct 02 '14 at 21:06
  • Instead of trying to write your own, instead I'd recommend actually looking at how the methods are written. You can do that by browsing through Ruby's source code, which is freely available, or by going to http://ruby-doc.org and clicking the "click to toggle source" links for methods you want to see. Some are implemented in Ruby and others are in C, but either way you'll see how they did it. – the Tin Man Oct 02 '14 at 21:08
  • @Max, if you are inclined and have the time to do so, please do feel free. My feelings won't be hurt, and I'll learn more than what I knew before. Please, though, do look at my edit that I just made. I appreciate any time you spend on clarification of my code for any reasons whatever. Thank you. – user273072545345 Oct 02 '14 at 21:13
  • @theTinMan, I'm actually finding this exercise interesting because it's forcing me to look at Enumerable's methods and trying to understand what it does. Your advice is good, but this is also another way of doing it. =) – user273072545345 Oct 02 '14 at 21:15
  • @lurker, this is the error I get when I try to execute the last one, ie, `p c.all?(@values) { |x| x >= 3 }`: EnumAssignmentv3.rb:23:in `block in
    ': undefined method `>=' for nil:NilClass (NoMethodError) from EnumAssignmentv3.rb:13:in `all?' from EnumAssignmentv3.rb:23:in `
    '
    – user273072545345 Oct 02 '14 at 21:15

1 Answers1

0

I'm not sure why you need enum at all? Enumerable is a module included in array, so if you're not familiar with this I recommend you read about "modules and mix-ins" in Ruby.

all? works simply by passing EACH of the array elements to the block. If there is ANY element (at least 1) for which the block returns false, then all? evaluates to false. Try analyzing this code:

class MyAllImplementation
  def initialize(array)
    @array = array
  end

  def my_all?
    @array.each do |element| # for each element of the array
      return true unless block_given? # this makes sure our program doesn't crash if we don't give my_all? a block.
      true_false = yield(element) # pass that element to the block
      return false unless true_false # if for ANY element the block evaluates to false, return false
    end
    return true # Hooray! The loop which went over each element of our array ended, and none evaluted to false, that means all elements must have been true for the block.
  end
end


a = MyAllImplementation.new([1,2,3])
p a.my_all? { |x| x > 0 } #=> true
p a.my_all? { |x| x > 1 } # false, because 1 is not bigger than 1, it's equal to 1
daremkd
  • 8,244
  • 6
  • 40
  • 66