Skip to content

Instantly share code, notes, and snippets.

@taras2358
taras2358 / flatten_array.rb
Last active December 11, 2018 13:47
Mixin for extending ruby array with :recursive_flatten method (as supposedly it should be used often). Used default tests (instead of rspec) for simplicity
# frozen_string_literal: true
class Array
module Mixins
# Mixing for providing flatten method into Array
module Flatten
# Returns a flatten copy of array
# Example: [[1,2,[3]],4].recursive_flatten => [1, 2, 3, 4]
def recursive_flatten(result: [])
each do |element|
@taras2358
taras2358 / Description.md
Last active March 22, 2017 11:33
Rails e-commerce exercise

Given you have Rails e-commerce website. There are Product and Category models which have has_and_belongs_to_many relationship.

models/product.rb

class Product
  has_and_belongs_to_many :categories
end

models/category.rb