As far as I understand the Array class already mixes in the Enumerable module.
If that's so, why isn't there [:example].next?
Why do I need to make it [:example].to_enum.next?
to_enum has nothing to do with Enumerable, it returns an Enumerator. Array doesn't have a next method because next is an Enumerator method, not an Enumerable method.
Because the Enumerable module is different from the Enumerator class.
Being "Enumerable" means that the class gets a bunch of freebie methods that create "Enumerators". Compare to Java's Iterable and Iterator interfaces.
Related
Here is a code sample from the ruby pickaxe book:
Class VowelFinder
include Enumerable
def initialize(string)
#string = string
end
def each
#string.scan(/[aeiou]/] do |vowel|
yield vowel
end
end
end
vf = VowelFinder.new("the quick brown fox jumped")
vf.inject(:+) # => 'euiooue'
What I am having a hard time understanding is where the modified 'each' method comes into play? I assume inject is calling it at some point but don't understand why it's doing it, and how I could predict or emulate this behavior in my code.
Thanks!
the VowelFinder class implements the protocol that is mandatory for the Enumerable module that you can include to gain a lot of ruby iterator methods: http://apidock.com/ruby/Enumerable
The Enumerable mixin provides collection classes with several
traversal and searching methods, and with the ability to sort. The
class must provide a method each, which yields successive members of
the collection. If Enumerable#max, #min, or #sort is used, the objects
in the collection must also implement a meaningful <=> operator, as
these methods rely on an ordering between members of the collection.
The call to inject actually causes a call to each. The way inject works is by going over all the elements of the Enumerable using each, applying the passed operator to the memo and to the next value and storing the result back into memo. The result of the entire call is the value of memo in the end. You can see the documentation in here.
You are correct that inject actually calls each method and the reason this happens is because inject actually uses each method internally. In fact, a lot if the Enumerable methods use the .each method; like #map, #select, #reject.
The reason for this is that .each is the actual method which allows looping in an Enumerable Object. The other methods are just so that things get easy for us, developers, and we don't have to use .each everywhere. Imagine having to write the above code using .each. It wouldn't be tough but vf.inject(:+) is definitely easier. So is the case with collect or map.
And the best way to implement this(as is done in Ruby) without repeating code is to have the other method call #each as there is no code duplication and each of #map or #collect or #inject does not have to traverse the Enumerable Object differently.
Genrally, I avoid using #each and there are so many other methods which makes our job easier. If you want to modify your array collection, it's advisable not to money-patch the #each method (unless, of course, you want all the other methods to change as well.)
I was working with Ruby and WIN32OLE (which is specifically, Excel).
I found all those enumerable objects, such as Range.Columns, are automatically enumerable in Ruby, and can be iterated using obj.each method.
I am wondering how it works? I understand that if you want to make something enumerable, you have to include "Enumerable". But apparently they cannot put that line in the OLE object. Is it just directly mapping obj.each method to for each loop?
There's an interesting set of posts here that might answer your question, specifically:
(...) each is called as
dynamically as any other OLE method; it's not determined before the
call whether or not the object actually implements IEnum.
and:
Enumerable#find method clashes the 'find' method of Excel Range
object. This is the (only) reason why WIN32OLE does not include
Enumerable.
WIN32OLE class has 'each' method (WIN32OLE#each is defined) (...)
Hope this helps!
What is the difference in getting an enum back or getting an enumerator back?
Is an enum an object and an enumerator an object that does stum to an enum?
When you say "getting an enum back" are you referring to the to_enum method of some classes? In this case "enum" is just short for "enumerable," i.e. to_enum returns an object of class Enumerable. Ruby's standard library has no other concept called "enum."
I have a class called Note, which includes an instance variable called time_spent. I want to be able to do something like this:
current_user.notes.inject{|total_time_spent,note| total_time_spent + note.time_spent}
Is this possible by mixing in the Enumerable module? I know you are supposed to do add include Enumerable to the class and then define an each method, but should the each method be a class or instance method? What goes in the each method?
I'm using Ruby 1.9.2
It's easy, just include the Enumerable module and define an each instance method, which more often than not will just use some other class's each method. Here's a really simplified example:
class ATeam
include Enumerable
def initialize(*members)
#members = members
end
def each(&block)
#members.each do |member|
block.call(member)
end
# or
# #members.each(&block)
end
end
ateam = ATeam.new("Face", "B.A. Barracus", "Murdoch", "Hannibal")
#use any Enumerable method from here on
p ateam.map(&:downcase)
For further info, I recommend the following article: Ruby Enumerable Magic: The Basics.
In the context of your question, if what you expose through an accessor already is a collection, you probably don't need to bother with including Enumerable.
The Enumerable documentations says the following:
The Enumerable mixin provides collection classes with several
traversal and searching methods, and with the ability to sort. The
class must provide a method each, which yields successive members of
the collection. If Enumerable#max, #min, or #sort is used, the objects
in the collection must also implement a meaningful <=> operator, as
these methods rely on an ordering between members of the collection.
This means implementing each on the collection. If you're interested in using #max, #min or #sort you should implement <=> on its members.
See: Enumerable
we can call the Array method in the top level like this
Array(something)
that makes sense to me, it's a method call without explicit receiver, and self, which is main in this case, is inserted at the front of the method call. But isn't it that this is equivalent to :
Kernel.Array(something)
this doesn't make sense to me. Since in the first case, the object main is of class Object, which got Kernel module mixed in, thus have the Array method. But in the second case, we are calling the Array method on the Kernel module object itself, rather than main object, didn't they are NOT the same thing?
sorry for my bad english.
Kernel.Array is what is known as a module function. Other examples of module functions include Math.sin, and Math.hypot and so on.
A module function is a method that is both a class method on the module and also a private instance method. When you invoke Array() at the top-level you are invoking it as a private instance method of the main object. When you invoke it through Kernel.Array() you are invoking it as a class method on Kernel. They are the same method.
To learn more, read up on the module_function method in rubydocs: http://www.ruby-doc.org/core/classes/Module.html#M001642
class Object mixed-in module Kernel, but Kernel is an instance of Object. So Kernel "module" methods - is it's instance methods.
What's confusing you is the difference between class and instance methods.
Class methods don't have an explicit receiver, and thus no self to access other fields with. They just... are.
Generally instance methods are used to query or manipulate the attributes of a given object, whereas the class methods are "helper" or "factory" methods that provide some functionality associated with or especially useful for a certain kind of class, but not dependent on actual live instances (objects) of that class.
Not sure about Ruby, but Java has (for example) a whole class, Math that contains nothing but instance methods like sin(), max(), exp() and so forth: There is no "Math" object, these are just methods that embody mathematical algorithms. Not the best example, because in Ruby those methods are probably embedded right into the numeric classes as instance methods.
The case you mention is a bit confusing because Array's () method and Kernel's Array() method are in fact different methods that do similar things. Both are class methods.
Array() takes a list of arguments and makes and returns an array containing them.
Kernel.Array() takes a single argument of an "array-able" type, such as a sequence, and takes the values returned by this argument and builds an array from those.
UPDATE
The downvote was perhaps justified; I apologize for taking on a subject outside my area of expertise. I think I'll be deleting this answer soon.
# Chuck: I would sincerely hope that a language/library's official documentation would offer some meaningful clues as to how it works. This is what I consulted in answering this question.
The rdoc for Kernel.Array():
Returns arg as an Array. First tries to call arg.to_ary, then arg.to_a. If both fail, creates a single element array containing arg (unless arg is nil).
for Array.():
Returns a new array populated with the given objects.
I don't know about you, but I think if the docs vary that much then either they're talking about separate methods or the documentation is a train wreck.
# freeknight:
But everything in ruby is an object of some kind, even classes and modules. And Kernel.Array is actually a method call on an specific object - the Kernel object.
Yeah, under the covers it's similar in Java too. But the Array() method isn't doing anything with Kernel, any more than Array() is doing anything with the Array class object, so this is really only a semantic quibble. It's an instance method because you could hang it off class IPSocket if you were crazy enough, and it would still work the same way.
They are the same thing:
a = Kernel.Array('aa')
=> ["aa"]
a.class
=> Array
a = Array('aaa')
=> ["aaa"]
a.class
=> Array
Maybe there is an alias?