Make method calls chainable - ruby

I have a Ruby class, and each method on it keeps indices of an array of hashes based on certain conditions.
For example (code has been edited since original posting)
module Dronestream
class Strike
class << self
...
def strike
#strike ||= all
end
def all
response['strike'] # returns an array of hashes, each individual strike
end
def in_country(country)
strike.keep_if { |strike| strike['country'] == country }
self
end
def in_town(town)
strike.keep_if { |strike| strike['town'] == town }
self
end
...
end
end
This way, you can do Dronestream::Strike.in_country('Yemen'), or Dronestream::Strike.in_town('Taizz'), and each returns an array. But I'd like to be able to do Dronestream::Strike.in_country('Yemen').in_town('Taizz'), and have it return only the strikes in that town in Yemen.
But as of now, each separate method returns an array. I know that if I have them return self, they'll have the method I need. But then they won't return an array, and I can't call, for example, first or each on them, like I could an array, which I need to do. I tried to make Strike < Array, but then, first is an instance method on Array, not a class method.
What should I do?
EDIT
Here is a part of my test suite. Following the answer below, the tests pass individually, but then fail.
describe Dronestream::Strike do
let(:strike) { Dronestream::Strike }
before :each do
VCR.insert_cassette 'strike', :record => :new_episodes
#strike = nil
end
after do
VCR.eject_cassette
end
...
# passes when run by itself and when the whole file runs together
describe '#country' do
let(:country_name) { 'Yemen' }
it 'takes a country and returns strikes from that country' do
expect(strike.in_country(country_name).first['country']).to eq(country_name)
end
end
# passes when run by itself, but fails when the whole file runs together
describe '#in_town' do
let(:town_name) { 'Wadi Abida' }
it 'returns an array of strikes for a given town' do
expect(strike.in_town(town_name).first['town'].include?(town_name)).to be_true
end
end
...
end

You can overwrite the method_missing to handle this.
Return self in your in_country or in_town method. Then when called first to it, delivery it to the all array to handle.
the code may be like this:
module Dronestream
class Strike
class << self
...
def all
...
end
def in_country(country)
all.keep_if { |strike| strike['country'] == country }
self
end
def in_town(town)
all.keep_if { |strike| strike['town'] == town }
self
end
...
def method_missing(name,*args,&block)
return all.send(name.to_sym, *args, &block) if all.respond_to? name.to_sym
super
end
end

Related

Can I use a .select(&) method here?

I'm writing some methods for an Artist class, one finds the songs that belong to the artist and the other finds the genres through the songs. I recently found out about the songs.map(&:genre) syntax as opposed to writing songs.map { |song| song.genre } and it got me a little curious if I could use the same styling for my songs method.
def songs
Song.all.select { |song| song.artist == self}
end
def genres
songs.map(&:genre)
end
The unary ampersand in block position simply calls to_proc on whatever object it's given. Symbol#to_proc produces exactly what you just described; it creates a Proc which takes a single argument and calls the method with the given symbol on it, so :genre.to_proc is a procedure which calls the genre method on an arbitrary object.
The built-in Symbol#to_proc is insufficient to do what you're doing for songs; you want to access something and compare it. But, of course, to_proc can do whatever we want, so we could make a class that does this.
class MatchesArtist
attr_reader :artist
def initialize(artist)
#artist = artist
end
def to_proc
Proc.new { |song| song.artist == self.artist }
end
end
def songs
matches = MatchesArtist.new(self)
Song.all.select(&:matches)
end
Now, this can be useful in general, if you're building up combinators using some kind of query language. But for the code you've shown, it's likely that an explicit block is much clearer.
You don't need a whole class with a #to_proc for this, you can just write a method that returns lambda:
def matches_artist
->(song) { song.artist == artist }
end
and then say:
Song.all.select(&matches_artist)
Or even nicer (IMO):
def matches(artist)
->(song) { song.artist == artist }
end
and say:
Song.all.select(&matches(artist))
Or even a lambda that returns a lambda:
matches = ->(artist) { ->(song) { song.artist == artist } }
Song.all.select(&matches[artist]))

Ruby - Singleton module with each. Returning a value gives an Enumerator. How can I get the value instead?

Example code:
module Creatures
class << self
def to_h
{
squirtle: {full_name: 'Squirtle T. Turtle'},
pikachu: {full_name: 'Pikachu B. Pikachu'}
}
end
def keys
to_h.keys
end
def collect
to_h.keys.collect
end
def each
to_h.keys.each
end
end
end
module CompanionHelper
def get_companion_creature_experience(companion_data)
Creatures.each do |creature|
return companion_data[creature]["#{creature}_experience".to_sym] if companion_data.has_key?(creature)
end
end
end
include CompanionHelper
companion_data = {squirtle: {squirtle_experience: 8000}}
get_companion_creature_experience(companion_data)
Forgive me if the example is contrived. The original code is from the insurance world but I can't copy and paste it :)
The crux of the problem is I want to use Creatures.each in another module, pass it a block, and have it work just like Creatures.keys.each would work (i.e. w/ the given example companion data I get 8000 for get_companion_creature_experience(companion_data).
Currently I get Enumerator instead.
Problem is that to_h.keys.each returns Enumerator which does not expect any arguments. Pass a block inside each since you want to use it:
def each &block
to_h.keys.each &block
end
Or you can yield it:
def each
to_h.keys.each do |k|
yield k
end
end

Ruby custom iterators

I have a class game which contains some arrays of custom objects (dinosaurs, cacemen etc.), that are returned by different accessors, such as game.dinosaurs, game.cavemen etc.
At present, all these accessors just return the internally stored arrays. But now I'd like to add some custom iteration methods to these arrays returned by those accessors, to be able to write code such as game.dinosaurs.each_carnivore { ... } etc. similarly to each_element and each_attr iterators in LibXML::XML::Node. But the objects returned from my accessors game.dinosaurs and game.cavemen have to behave like arrays still.
How are things like that usually done in Ruby?
Should I make the objects returned from my accessors to be some custom classes derived from Ruby's Array class? Or maybe should I just create a custom class with Enumerable mixed in?
I know I can use map or select externally on my collections, but I wanted to encapsulate these iterations internally that my class's users won't need to bother how to set up an iteration to select only carnivore dinosaurs from the internal array.
Edit: I'm not asking about how to use iterators or how to implement them, but how to add just some custom iterators to object which previously were just plain arrays (and still need to be).
It depends (as always). You could use an array subclass and you you could build a custom class and use composition and delegation. Here's a simple example with an array subclass:
class DinosaurArray < Array
def carnivores
select { |dinosaur| dinosaur.type == :carnivore }
end
def herbivores
select { |dinosaur| dinosaur.type == :herbivore }
end
def each_carnivore(&block)
carnivores.each(&block)
end
def each_herbivore(&block)
herbivores.each(&block)
end
end
And here's a simple one with composition and delegation:
class DinosaurArray
def initialize
#array = []
end
def <<(dinosaur)
#array << dinosaur
end
def carnivores
#array.select { |dinosaur| dinosaur.type == :carnivore }
end
def herbivores
#array.select { |dinosaur| dinosaur.type == :herbivore }
end
def each(&block)
#array.each(&block)
end
def each_carnivore(&block)
carnivores.each(&block)
end
def each_herbivore(&block)
herbivores.each(&block)
end
end
Both implementation can be used like this:
require 'ostruct'
dinosaurs = DinosaurArray.new
dinosaurs << OpenStruct.new(type: :carnivore, name: "Tyrannosaurus")
dinosaurs << OpenStruct.new(type: :carnivore, name: "Allosaurus")
dinosaurs << OpenStruct.new(type: :herbivore, name: "Apatosaurus")
puts "Dinosaurs:"
dinosaurs.each.with_index(1) { |dinosaur, i| puts "#{i}. #{dinosaur.name}" }
puts
But also has custom iterators:
puts "Carnivores:"
dinosaurs.each_carnivore.with_index(1) { |dinosaur, i| puts "#{i}. #{dinosaur.name}" }
puts
puts "Herbivores:"
dinosaurs.each_herbivore.with_index(1) { |dinosaur, i| puts "#{i}. #{dinosaur.name}" }
Output:
Dinosaurs:
1. Tyrannosaurus
2. Allosaurus
3. Apatosaurus
Carnivores:
1. Tyrannosaurus
2. Allosaurus
Herbivores:
1. Apatosaurus
You can do this via using ruby blocks. Read more
Simple example here:
class Game
def initialize
#carnivoures = [1,2,3]
end
def each_carnivoures
#carnivoures.each do |carni|
yield carni
end
end
end
Game.new.each_carnivoures{ |c| p c}
It also would be nice to have a possibility for chaining such filters. You can achieve this simply by wrapping select method into custom one, returning your new class instead of array. You may wrap some other methods as well, e.g. map:
class Units < Array
def select
self.class.new(super)
end
def dinosaurs
select{ |unit| unit.kind == 'dinosaur' }
end
def cavemen
select{ |unit| unit.kind == 'caveman' }
end
def carnivore
select{ |unit| unit.type == 'carnivore' }
end
def herbivore
select{ |unit| unit.type == 'herbivore' }
end
end
Units.dinosaurs.carnivore
Units.cavemen.herbivore

Validate response from a yielded block

Assume I am testing the following class:
class Processor
def initialize(tree)
#tree = tree
end
def process(entity)
#tree.each_branch do |branch|
branch.inject({}) do |result, fruit|
result[fruit.name] = fruit.type == entity.type
end
end
end
end
I'd like to inject a stubbed tree, in my spec I would have:
describe Processor do
let(:tree) { double("tree") }
let(:apple) { Fruit.new("apple") }
let(:processor) { Processor.new(tree) }
let(:fruit1) { Fruit.new("orange") }
let(:fruit2) { Fruit.new("apple") }
it "should process a fruit"
tree.stub(:each_branch).and_yield([fruit1, fruit2])
Processor.process(apple)
end
end
I would expect the following hash to be created in the block. How do I verify that it is created correctly and returned to the caller of the block?
{ "orange" => false, "apple" => true }
EDIT: I omitted details of the Fruit class, it should be irrelevant.
If you're ever having to try and catch the result somewhere in the middle of a method that your testing, it's normally a good sign that you need to refactor.
Here's an example: add a method to the branch, then test the branch class (assuming it's a class that you're in control of).
class Branch
def unique_fruits
inject({}) do |result, fruit|
result[fruit.name] = fruit.type == entity.type
end
end
end
class Processor
# snip ...
def process(entity)
#tree.each_branch do |branch|
branch.unique_fruits
end
end
end
That's easier to test, as inject returns the hash. You can write a unit test for the branch class and isolate that method. Then in the Processor#process method you replace the inject block with a call to branch.unique_fruits.
If you don't have control over branch, just extract the block to another method on the Processor class:
class Processor
# snip...
def process(entity)
#tree.each_branch do |branch|
unique_fruits_on_branch(branch)
end
end
def unique_fruits_on_branch(branch)
branch.inject({}) do |result, fruit|
result[fruit.name] = fruit.type == entity.type
end
end
end
However, you can probably see that it doesn't look as nice - the Processor class is working at different levels of abstraction. But both of these are easier to unit test.

How to create a method like ".find_by_something_and_something_else" using Ruby?

Using Ruby I know you can get pretty creative with how you name your methods. For instance in rails you have .find_by_this_and_that.
How can I do this?
Example:
def get_persons_with_5_things
res = []
persons.each do |person|
if person.number_of_things == %MAGICALLY GET THE NUMBER 5 FROM FUNCTION NAME%
res << person
end
end
return res
end
I'm not even sure how you call this kind of things so any pointers would be appreciated.
I'm a little confused by your example. If you define the method with the hardcoded 5 in the method name, then you don't need to magically figure it out inside the body of the method. If you want to do something dynamic with method missing, it would be something like this:
def method_missing(name, *args)
if name.to_s =~ /get_persons_with_(\d+)_things/
number_of_things = $1.to_i
res = []
persons.each do |person|
if person.number_of_things == number_of_things
res << person
end
end
return res
else
return super(name, *args)
end
end
[EDIT (Jörg W Mittag)]: This is a more Rubyish way of implementing that same method:
def method_missing(name, *args)
return super unless name.to_s =~ /get_persons_with_(\d+)_things/
number_of_things = $1.to_i
return persons.select {|person| person.number_of_things == number_of_things }
end
super without any arguments just passes the original arguments along, no need to pass them explicitly
an early return guarded by a trailing if or unless expression greatly clears up control flow
all the each iterator does, is select items according to a predicate; however, there already is an iterator for selecting items: select
Ruby has different meta programming techniches to do this kind of stuff.
First we need our variable method
class DB
def get_persons_with_x_things(x)
res = []
persons.each do |person|
if person.number_of_things == x
res << person
end
end
return res
end
end
define_method
If there is a finite number of x's. We could use define_method to create all this methods. define_method creates a method. The first argument is the name of the method, the seccond argument or the given block is the stuff, which get's executed when the method is called.
This way, you don't realy create such method's, but It will look for the user if he calls it, as if it existed. But if the user relies on Object#methods and such, he will never see your inifinite number of fake methods.
class DB
99.times do |i|
define_method("get_persons_with_#{i}_things") do
get_persons_with_x_things(i)
end
end
end
method_missing
If there is an infinite numbor of x's method_missing would be better suited for this Task. If someone tries to call a method which does not exist, method_missing is executed instead. The first argument for method_missing is the method name as symbol, the following arguments are the original arguments.
class DB
def method_missing(name, *args)
case name.to_s
when /^get_persons_with_(\d+)_things$/
get_persons_with_x_things($1.to_i)
else
super(name, *args)
end
end
end
method_missing and send
To not use static regexe would be even cooler. But this could have some security implications. The method send I use here, calls a method by it's name.
class DB
def method_missing(name, *args)
name.to_s=~ /\d+/
# always be carefull with $ variables, they are global for this thread, so save everything as fast as you can
new_name= "#{$`}x#{$'}"
number= $1.to_i
if method_defined?(new_name)
send(new_name, number)
else
super(name, *args)
end
end
end
you can do a lot of things like this with method missing:
Ruby Docs
StackOveflow method_missing
Have a look at Ruby's callbacks specially method_missing.

Resources