Active Record order by group size - ruby

I've got an active record query where I'm using group_by
#foo = Foo.group_by(&:relation)
Then in the view i'm using
#foo.each do |group, values|
group x has values.count elements
end
Is there a way I could sort these by the count of each group?

group_by is not a ActiveRecord method, group is. group_by is a Enumerator method.
What about
#foo = Foo.group('relation').order('count_id asc').count('id')
Taken from "Order by" result of "group by" count?.
Otherwise, if you want to sort it on Ruby level, you could do
disordered_hash = {:two=>[1, 2], :one=>[1], :three=>[1, 2, 3]}
ordered_array = disordered_hash.sort {|k, v| k[1].count <=> v[1].count} # add .reverse if you want
# => [[:one, [1]], [:two, [1, 2]], [:three, [1, 2, 3]]]

Related

Reassign entire array to the same reference

I've searched extensively but sadly couldn't find a solution to this surely often-asked question.
In Perl I can reassign an entire array within a function and have my changes reflected outside the function:
#!/usr/bin/perl -w
use v5.20;
use Data::Dumper;
sub foo {
my ($ref) = #_;
#$ref = (3, 4, 5);
}
my $ref = [1, 2];
foo($ref);
say Dumper $ref; # prints [3, 4, 5]
Now I'm trying to learn Ruby and have written a function where I'd like to change an array items in-place by filtering out elements matching a condition and returning the removed items:
def filterItems(items)
removed, items = items.partition { ... }
After running the function, items returns to its state before calling the function. How should I approach this please?
I'd like to change an array items in-place by filtering out elements matching a condition and returning the removed items [...] How should I approach this please?
You could replace the array content within your method:
def filter_items(items)
removed, kept = items.partition { |i| i.odd? }
items.replace(kept)
removed
end
ary = [1, 2, 3, 4, 5]
filter_items(ary)
#=> [1, 3, 5]
ary
#=> [2, 4]
I would search for pass by value/reference in ruby. Here is one I found first https://mixandgo.com/learn/is-ruby-pass-by-reference-or-pass-by-value.
You pass reference value of items to the function, not the reference to items. Variable items is defined out of method scope and always refers to same value, unless you reassign it in the variable scope.
Also filterItems is not ruby style, see https://rubystyle.guide/
TL;DR
To access or modify an outer variable within a block, declare the variable outside the block. To access a variable outside of a method, store it in an instance or class variable. There's a lot more to it than that, but this covers the use case in your original post.
Explanation and Examples
In Ruby, you have scope gates and closures. In particular, methods and blocks represent scope gates, but there are certainly ways (both routine and meta) for accessing variables outside of your local scope.
In a class, this is usually handled by instance variables. So, as a simple example of String#parition (because it's easier to explain than Enumerable#partition on an Array):
def filter items, separator
head, sep, tail = items.partition separator
#items = tail
end
filter "foobarbaz", "bar"
#=> "baz"
#items
#=> "baz"
Inside a class or within irb, this will modify whatever's passed and then assign it to the instance variable outside the method.
Partitioning Arrays Instead of Strings
If you really don't want to pass things as arguments, or if #items should be an Array, then you can certainly do that too. However, Arrays behave differently, so I'm not sure what you really expect Array#partition (which is inherited from Enumerable) to yield. This works, using Enumerable#slice_after:
class Filter
def initialize
#items = []
end
def filter_array items, separator
#items = [3,4,5].slice_after { |i| i == separator }.to_a.pop
end
end
f = Filter.new
f.filter_array [3, 4, 5], 4
#=> [5]
Look into the Array class for any method which mutates the object, for example all the method with a bang or methods that insert elements.
Here is an Array#push:
ary = [1,2,3,4,5]
def foo(ary)
ary.push *[6, 7]
end
foo(ary)
ary
#=> [1, 2, 3, 4, 5, 6, 7]
Here is an Array#insert:
ary = [1,2,3,4,5]
def baz(ary)
ary.insert(2, 10, 20)
end
baz(ary)
ary
#=> [1, 2, 10, 20, 3, 4, 5]
Here is an example with a bang Array#reject!:
ary = [1,2,3,4,5]
def zoo(ary)
ary.reject!(&:even?)
end
zoo(ary)
ary
#=> [1, 3, 5]
Another with a bang Array#map!:
ary = [1,2,3,4,5]
def bar(ary)
ary.map! { |e| e**2 }
end
bar(ary)
ary
#=> [1, 4, 9, 16, 25]

The order of array elements when calling .to_a of Struct object in Ruby

.to_a of Struct object returns an array of its attribute values.
How are the array elements ordered?
It seems to be the definition order of attributes when running Struct.new, but I don't have much confidence.
S = Struct.new :foo, :bar, :baz
# => S
s = S.new 1, 2, 3
# => #<struct S foo=1, bar=2, baz=3>
s.to_a
# => [1, 2, 3]
Any idea?

How to get reference to object you're calling in method?

I am trying to create a method for objects that I create. In this case it's an extension of the Array class. The method below, my_uniq, works. When I call puts [1, 1, 2, 6, 8, 8, 9].my_uniq.to_s it outputs to [1, 2, 6, 8].
In a similar manner, in median, I'm trying to get the reference of the object itself and then manipulate that data. So far I can only think of using the map function to assign a variable arr as an array to manipulate that data from.
Is there any method that you can call that gets the reference to what you're trying to manipulate? Example pseudo-code that I could replace arr = map {|n| n} with something like: arr = self.
class Array
def my_uniq
hash = {}
each do |num|
hash[num] = 0;
end
hash.keys
end
end
class Array
def median
arr = map {|n| n}
puts arr.to_s
end
end
Thanks in advance!
dup
class Array
def new_self
dup
end
def plus_one
arr = dup
arr.map! { |i| i + 1 }
end
def plus_one!
arr = self
arr.map! { |i| i + 1 }
end
end
array = [1, 3, 5]
array.new_self # => [1, 3, 5]
array.plus_one # => [2, 4, 6]
array # => [1, 3, 5]
array.plus_one! # => [2, 4, 6]
array # => [2, 4, 6]
dup makes a copy of the object, making it a safer choice if you need to manipulate data without mutating the original object. You could use self i.e. arr = self, but anything you do that changes arr will also change the value of self. It's a good idea to just use dup.
If you do want to manipulate and change the original object, then you can use self instead of dup, but you should make it a "bang" ! method. It is a convention in ruby to put a bang ! at the end of a method name if it mutates the receiving object. This is particularly important if other developers might use your code. Most Ruby developers would be very surprised if a non-bang method mutated the receiving object.
class Array
def median
arr = self
puts arr.to_s
end
end
[1,2,3].median # => [1,2,3]

Create own Array#map like method

I want to create an instance method like Array#my_map and that method should behavior of the original Array#map method.
I want to same output from new method as below:
arr = [1, 2, 3, 4]
arr.new_map_method do |x|
x + 1
end # => [2, 3, 4, 5]
arr.new_map_method(&:to_s) # => ["1", "2", "3", "4"]
The easiest way of creating a method with the same behavior of a different method is a simple alias:
class Array
alias_method :new_map_method, :map
end
If, for whatever strange reason, you don't want to use map, you can use inject instead:
class Array
def new_map_method
return enum_for(__callee__) unless block_given?
inject([]) {|acc, el| acc << yield(el) }
end
end
Thanks guys! I also made one solution for my qeestion
class Array
def my_map(&block)
result = []
each do |element|
result << block.call(element)
end
result
end
end
function calling
[1,2,3].my_map(&:to_s)
output => ["1", "2", "3"]
[1, 2, 3, 4, 5].my_map do |x|
x
end
output => [1, 2, 3, 4, 5]

group_by one attribute and map another attribute?

I have a list of objects with attributes A and B. Is there a one-liner way to store B values in arrays grouped by A values, in one loop? (not group_by, and then map) I'd have, as a result, an array of arrays.
result[a1] = [b1, b4, b5]
Edit : apparently i wasn't clear enough, sorry.
I have a list of objects like this :
class MyObject
attr_accessor :attrA, :attrB
end
and i'd like a method that takes a list of MyObjects and returns all the attrB values grouped by attrA values.
Kristjan answer is good, and if there are no one liner doing this, it will be the accepted answer
Is a two-liner ok? Set up a results hash with a default empty array value, then loop through your items concatenating the value you want into the group you want.
list = [
['a', 1],
['b', 2],
['a', 3],
['b', 4]
]
result = Hash.new { |h, k| h[k] = [] }
list.each do |item|
result[item.first] << item.last
end
puts result.inspect
# {"a"=>[1, 3], "b"=>[2, 4]}

Resources