How can I detect arrays or hashes that include a recursive structure like a, b, and c below?
Simplest instance of recursive array
a = []
a[0] = a
a # => [[...]]
Recursion cycle/depth is not one
b = [[], :foo]
b[0][0] = b
b # => [[[...]], :foo]
Recursion at a non-root level
c = [a, :foo]
c # => [[...], :foo]
I love recursion.
Here's a decent way, iterating through everything and keeping a hash of objects you have seen (for fast lookup)
class Object
def is_recursive?(known = {})
false
end
end
module Enumerable
def is_recursive?(known = {})
return true if known.include?(self)
known[self] = true
begin
any? do |*args|
args.any?{|item| item.is_recursive?(known)}
end
ensure
known[self] = false
end
end
end
x = []; x << x
p x.is_recursive? # => true
p ({x => 42}).is_recursive? # => true
p [{foo: x}].is_recursive? # => true
p [[[[[[:foo], {bar: [42]}]]]]].is_recursive? # => false
Mind you, this is a bit rough and you could run into trouble. For example you'd have endless loops with [1..Float::INFINITY].is_recursive?, although that's easily salvable with
class Range
def is_recursive?(known = {})
false # optimization
end
end
You can't flatten a recursive array, so you could check it by:
begin
a.flatten
rescue ArgumentError => e
e.to_s =~ /recursive/
end
You are looking for the method include?
a = []
a[0] = a
a.include? a
=> true
But this don't works for nested arrays, as your second example. You can do this recursivelly:
def check_recursive(array, target = nil)
target ||= array
return true if array.include?(target)
array.any? do |obj|
if obj.kind_of? Array
check_recursive(obj, target)
obj.include?(target)
end
end
end
There are basically two abordages to find it recursive: Look in depth or deep-first search or breadth-first search. The best solution depends on your problem. My example implements deep-frist search, which usually it is a good idea.
Related
I'm on Ruby 2.2.1 and have the following situation:
a = ... # some object
h = ... # some hash
p h.size #=> 1
p h.keys.first.hash == a.hash #=> true
p h.keys.first.eql?(a) #=> true
p h.has_key?(a) #=> false
How is this possible? I thought that the hashes matching and eql? returning true were the only conditions for keys to be considered equal.
Edit: Here's the full program. But please note that I'm not asking how to fix it—I know how. I'm asking why Ruby behaves that way! Because I'm confused as to why the API contracts of Hash count for nothing in this situation.
class A
attr_reader :x
def initialize(x)
#x = x
end
MY_HASH = { A.new(5) => 'foo' }
def ==(other)
#x == other.x
end
alias_method :eql?, :==
def hash
#x
end
end
a = A.new(5)
h = A::MY_HASH
p h.size #=> 1
p h.keys.first.hash == a.hash #=> true
p h.keys.first.eql?(a) #=> true
p h.has_key?(a) #=> false
At the time when you create MY_HASH the new hash function of A is not yet defined so MY_HASH will use the default one when creating an index of it's values. When you later define a new hash function it will change how the objects are hashed BUT NOT AUTOMATICALLY update the index in the already existing Hash MY_HASH.
You solve this my initializing MY_HASH after you have defined the new hash method for class A or by running MY_HASH.rehash
p h.has_key?(a) #=> false
A::MY_HASH.rehash
p h.has_key?(a) #=> true
I want to define a method which can take an optional amount of arguments and hashes, like so
def foo(*b, **c)
2.times.map.with_index { |i|
new_hash, new_array = {}, b
c.map { |key, value| new_hash[key] = value[i] unless value[i].nil? }
new_array << new_hash if new_hash.length > 0
send(:bar, new_array)
}
end
def bar(*b)
p b
end
If I've understood the splat and double splat operators correctly (which I doubt), then this should send the array b to the bar method, and only adding the new_hash from foo if it contains something. However, something weird happens - I'll try and illustrate with some snippets below
# invoking #foo
foo(a, key: 'value')
# first iteration of loop in #foo
# i is 0
# b is []
# c is { :key => ['value1'] }
# send(:bar, new_array) => send(:bar, [{:key => 'value1'}])
# bar yields: [{:key => 'value1'}]
Now, however, something happens
# second iteration of loop in #foo
# i is 1
# b is [:key => 'value1'] <---- why?
# c is { :key => ['value1']
Why has the value of b changed inside the loop of foo?
edit Updated the code to reflect a new array is created for each iteration
new_hash, new_array = {}, b
This doesn't create a copy of b. Now new_array and b point to the same object. Modifying one in-place will modify the other.
new_array << new_hash
That modifies new_array (and thus b) in place, so the new element remains on the next iteration. Use something like +, which creates a copy:
send(:bar, *(b + (new_hash.empty? ? [] : [new_hash])))
I need to iterate over an array and apply a supplied block to each element, and return the first true value returned by the block, which implies that I need to stop immediately as soon as I get a true value.
below is my code. I am a ruby newbie, and I am not sure if this code is reinventing the wheel. Maybe there is a library method or methods that can do that already? or may be this code can be simplified?
RS = {
:x => %w(\d+ a\d+ bb\d+ ccc\d+).map{|x| /^#{x}$/},
:y => %w(\w+ 1\w+ 22\w+ 333\w+).map{|x| /^#{x}$/}
}.freeze
def find s, t
r = RS[s]
if r
r.each do |p|
m = p.match t
return m if m
end
nil
end
end
p find :x, 'bb12345'
If you want the result of the block you could do it this way. This will iterate over the whole array, but wont evaluate any matches after the first one.
def find(s,t)
RS[s].inject(nil) {|m, p| m || p.match(t)}
end
You can break out early doing something like this
RS[s].inject(nil) {|m, p| (m && (break m)) || p.match(t)}
This is duplicated with: Ruby - Array.find, but return the value the block
You want a lazy map:
[nil, 1, 2, 3].lazy.map{|i| i && i.to_s}.find{|i| i}
# => "1"
Hopefully still actual: here a solution using detect, i made it possible to verbose the output so you can see which expressions are evaluated before returning a hit.
def find_match symbol, string , verbose = false, match = nil
if verbose
RS.detect{|x,v|x==symbol;v.detect{|re|puts re;match=string.match(/#{re}/)}}
else
RS.detect{|x,v|x==symbol;v.detect{|re|match=string.match(/#{re}/)}}
end
match
end
p find_match :x, 'bb12345'
p find_match :x, 'ee12345' , true #verbose output
p find_match :x, '12345'
p find_match :y, '22abcd'
#<MatchData "bb12345">
(?-mix:^\d+$)
(?-mix:^a\d+$)
(?-mix:^bb\d+$)
(?-mix:^ccc\d+$)
(?-mix:^\w+$)
#<MatchData "ee12345">
#<MatchData "12345">
#<MatchData "22abcd">
If your regex patterns are simple, you can just apply the regex again at the end, maybe.
Something like:
def find(s,t)
r = RS[s] and r.find{|p| p.match(t)}.try(:match, t)
end
Although it makes one redundant call to match, it is easier to understand.
First, find the pattern you want, then use that pattern.
I'm working with a Ruby API that takes a series of boolean switches, something along the lines of:
validate({ :can_foo => true, :can_bar => false, :can_baz => true, ... })
I'm writing a series of tests to verify that the API is behaving as it should, so I need to construct a lot of sets of switches. It seemed wasteful to continue to type :foo => true all the time, so I figured I'd write a little Ruby ditty to convert an array to this structure, e.g.
true_vals = %w( these are my true items )
false_vals = %w( these are my false items )
convert = lambda{ |arr, truthiness| arr.inject({}){ |res, key| res.update(key=>truthiness) } }
falsify = lambda{ |arr| convert.call(arr, false) }
truthify = lambda{ |arr| convert.call(arr, true) }
validate( truthify.call(true_vals).merge( falsify.call(false_vals) ) )
Does that seem any better than simply typing out a long list of :sym => [true|false] pairs? Is there a better way to do this?
(I started with true_vals.inject({}){ |res, key| res.update(key=>true) } but that doesn't feel DRY enough; I'd have to copy-paste & s/true/false/ to do the false ones; and I'm doing it many many times so a lambda seems reasonable)
Thanks,
--
Matt
cs = { true => [:y, :yes],
false => [:n, :no] }
Hash[cs.map{ |k, vs| vs.map{ |v| [v, k] } }.flatten(1)]
#=> {:y=>true, :yes=>true, :no=>false, :n=>false}
Here is one solution:
switches={}
true_vals.each do |v|
switches[v]=true
end
false_vals.each do |v|
switches[v]=false
end
Is there a good way to chain methods conditionally in Ruby?
What I want to do functionally is
if a && b && c
my_object.some_method_because_of_a.some_method_because_of_b.some_method_because_of_c
elsif a && b && !c
my_object.some_method_because_of_a.some_method_because_of_b
elsif a && !b && c
my_object.some_method_because_of_a.some_method_because_of_c
etc...
So depending on a number of conditions I want to work out what methods to call in the method chain.
So far my best attempt to do this in a "good way" is to conditionally build the string of methods, and use eval, but surely there is a better, more ruby, way?
You could put your methods into an array and then execute everything in this array
l= []
l << :method_a if a
l << :method_b if b
l << :method_c if c
l.inject(object) { |obj, method| obj.send(method) }
Object#send executes the method with the given name. Enumerable#inject iterates over the array, while giving the block the last returned value and the current array item.
If you want your method to take arguments you could also do it this way
l= []
l << [:method_a, arg_a1, arg_a2] if a
l << [:method_b, arg_b1] if b
l << [:method_c, arg_c1, arg_c2, arg_c3] if c
l.inject(object) { |obj, method_and_args| obj.send(*method_and_args) }
You can use tap:
my_object.tap{|o|o.method_a if a}.tap{|o|o.method_b if b}.tap{|o|o.method_c if c}
Sample class to demonstrate chaining methods that return a copied instance without modifying the caller.
This might be a lib required by your app.
class Foo
attr_accessor :field
def initialize
#field=[]
end
def dup
# Note: objects in #field aren't dup'ed!
super.tap{|e| e.field=e.field.dup }
end
def a
dup.tap{|e| e.field << :a }
end
def b
dup.tap{|e| e.field << :b }
end
def c
dup.tap{|e| e.field << :c }
end
end
monkeypatch: this is what you want to add to your app to enable conditional chaining
class Object
# passes self to block and returns result of block.
# More cumbersome to call than #chain_if, but useful if you want to put
# complex conditions in the block, or call a different method when your cond is false.
def chain_block(&block)
yield self
end
# passes self to block
# bool:
# if false, returns caller without executing block.
# if true, return result of block.
# Useful if your condition is simple, and you want to merely pass along the previous caller in the chain if false.
def chain_if(bool, &block)
bool ? yield(self) : self
end
end
Sample usage
# sample usage: chain_block
>> cond_a, cond_b, cond_c = true, false, true
>> f.chain_block{|e| cond_a ? e.a : e }.chain_block{|e| cond_b ? e.b : e }.chain_block{|e| cond_c ? e.c : e }
=> #<Foo:0x007fe71027ab60 #field=[:a, :c]>
# sample usage: chain_if
>> cond_a, cond_b, cond_c = false, true, false
>> f.chain_if(cond_a, &:a).chain_if(cond_b, &:b).chain_if(cond_c, &:c)
=> #<Foo:0x007fe7106a7e90 #field=[:b]>
# The chain_if call can also allow args
>> obj.chain_if(cond) {|e| e.argified_method(args) }
Although the inject method is perfectly valid, that kind of Enumerable use does confuse people and suffers from the limitation of not being able to pass arbitrary parameters.
A pattern like this may be better for this application:
object = my_object
if (a)
object = object.method_a(:arg_a)
end
if (b)
object = object.method_b
end
if (c)
object = object.method_c('arg_c1', 'arg_c2')
end
I've found this to be useful when using named scopes. For instance:
scope = Person
if (params[:filter_by_age])
scope = scope.in_age_group(params[:filter_by_age])
end
if (params[:country])
scope = scope.in_country(params[:country])
end
# Usually a will_paginate-type call is made here, too
#people = scope.all
Use #yield_self or, since Ruby 2.6, #then!
my_object.
then{ |o| a ? o.some_method_because_of_a : o }.
then{ |o| b ? o.some_method_because_of_b : o }.
then{ |o| c ? o.some_method_because_of_c : o }
Here's a more functional programming way.
Use break in order to get tap() to return the result. (tap is in only in rails as is mentioned in the other answer)
'hey'.tap{ |x| x + " what's" if true }
.tap{ |x| x + "noooooo" if false }
.tap{ |x| x + ' up' if true }
# => "hey"
'hey'.tap{ |x| break x + " what's" if true }
.tap{ |x| break x + "noooooo" if false }
.tap{ |x| break x + ' up' if true }
# => "hey what's up"
Maybe your situation is more complicated than this, but why not:
my_object.method_a if a
my_object.method_b if b
my_object.method_c if c
I use this pattern:
class A
def some_method_because_of_a
...
return self
end
def some_method_because_of_b
...
return self
end
end
a = A.new
a.some_method_because_of_a().some_method_because_of_b()
If you're using Rails, you can use #try. Instead of
foo ? (foo.bar ? foo.bar.baz : nil) : nil
write:
foo.try(:bar).try(:baz)
or, with arguments:
foo.try(:bar, arg: 3).try(:baz)
Not defined in vanilla ruby, but it isn't a lot of code.
What I wouldn't give for CoffeeScript's ?. operator.
I ended up writing the following:
class Object
# A naïve Either implementation.
# Allows for chainable conditions.
# (a -> Bool), Symbol, Symbol, ...Any -> Any
def either(pred, left, right, *args)
cond = case pred
when Symbol
self.send(pred)
when Proc
pred.call
else
pred
end
if cond
self.send right, *args
else
self.send left
end
end
# The up-coming identity method...
def itself
self
end
end
a = []
# => []
a.either(:empty?, :itself, :push, 1)
# => [1]
a.either(:empty?, :itself, :push, 1)
# => [1]
a.either(true, :itself, :push, 2)
# => [1, 2]