#StackBounty: #c #ruby #ffi creating a Dynamic array class in ruby using FFI and C function

Bounty: 50

I would like to create my own dynamic array class in ruby (as a training).
The idea is to have a class DynamicArray that has a capacity (the number of elements it can hold at one given moment), a size (the number of elements that were actually pushed in the array at a given moment) and a static_array which is a static array of ints of a fixed sized. Whenever this static_array is full, we will create a new static array with twice the capacity of the original static_array and copy every elements inside the new static_array.
As there is no static array in ruby, my idea was to use FFI https://github.com/ffi/ffi. to create a function in c that creates a static array of int of size n then be able to use it in my ruby program.
I have very little knowledge in C and am having a hard time understanding the doc of FFI
Here’s what I have so far, a create_array.c file that defines my c function to create an array.

int * createArray ( int size )
  int array[size];
  return 0;


a create_array.h file (from what I understood of FFI, you need to put your c functions in a c library.):

int * createArray ( int size )

and this is my dynamic_array.rb file that would do something along this lines :

require 'ffi'
class DynamicArray
  extend FFI::Library
  ffi_lib "./create_array.h"
  attach_function :create_array, [:int], :int
  def initialize
    @size = 0
    @capacity = 1
    @current_index = 0
    @static_array = create_array(@capacity)

  def add(element)
    @size += 1
    resize_array if @size > @capacity
    @static_array[@current_index] = element
    @current_index += 1


  def resize_array
    @capacity = @capacity*2
    new_arr = create_array(@capacity)
    @static_array.each_with_index do |val, index|
      new_arr[index] = val
    @static_array = new_arr

Here are some tests for add and resize :

  def test_add
    dynamic_arr = DynamicArray.new
    assert_equal(1, dynamic_arr.static_array[0])
    assert_equal(2, dynamic_arr.static_array[1])

  def test_resize_array
    dynamic_arr = DynamicArray.new
    assert_equal(2, dynamic_arr.capacity)
    assert_equal(4, dynamic_arr.capacity)

Can you please explain me what I should do to make this work ?

Get this bounty!!!

#StackBounty: #ruby-on-rails #ruby #ruby-on-rails-5 #monkeypatching #rails-activestorage Monkey patching ActiveStorage::Attachment gets…

Bounty: 50

So I decided to add an url attr_accessor to ActiveStorage::Attachment objects.

In development the patch holds for a while until it seems to “have been lost”. Meaning it works for few minutes, then it does not work anymore. Then I need to restart the server in order to get the patch applied again. I believe I am not patching correctly and I would need advises in that mater.

Here is what I tried:


First try :

module ActiveStorageUrl
  extend ActiveSupport::Concern

  included do
    attr_accessor :url

ActiveStorage::Attachment.send :include, ActiveStorageUrl

Second try

class ActiveStorage::Attachment < ActiveRecord::Base
  attr_accessor :url

And by the way in both case it’s loaded with this:


require 'ext/active_storage/attachment'

So when it work I have no error message, but after a while the patch “diseapear” (lacking better terms), and I get the following error, telling me my attr_accessor is not there anymore. Rails must have reloaded ActiveStorage classes and my patch is lost.

Module::DelegationError in Products#images
url delegated to blob, but blob is nil

Get this bounty!!!