Tag: arrays

Select consecutive integers from array in Ruby

I want to select consecutive integers from an array, for example: Input: (doesn’t have to be sorted) array = [1, 3, 6, 7, 8, 9, 10, 12, 13, 17] Expected output: #=> [6, 7, 8, 9, 10, 12, 13] This is my code so far: array = [1, 3, 9, 6, 7, 10, 8, 12, […]

Ruby On Rails Returning Array NoMethodError

We’ve been continuously working on this issue for a few months and not getting far with it. Since this was first asked, we changed the code (based on what the original developer for the site suggested), but we are still not getting where we need to be. I’m relatively new to Ruby and am currently […]

sort an array of arrays based on number of occurences in ruby

I want this integer array to be sorted in the right order based on its number of occurrences. question = [[1, 7, 8, 9, 10, 11, 12, 19, 20, 21, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, […]

Creating a hash from two arrays with identical values in Ruby

I’m having issues creating a hash from 2 arrays when values are identical in one of the arrays. e.g. names = [“test1”, “test2”] numbers = [“1”, “2”] Hash[names.zip(numbers)] works perfectly it gives me exactly what I need => {“test1″=>”1”, “test2″=>”2″} However if the values in “names” are identical then it doesn’t work correctly names = […]

Dividing text article to smaller parts with paging in Ruby on Rails

This time I’ve got problem with dividing text article into smaller parts. I don’t need to figure out “automatic” algorithm based on words counting or something. All I need is something similar to function which is build-in WordPress WYSIWYG editor (special breaking page tag). I thought out only one solution so far. I don’t want […]

Rails 4 / postgresql 9.4 – Query and update a ruby object inside an array

I have a method (find_by_sql) that outputs a ruby object inside an array like this: [#<deal id: 66480, opportunity_id: 4, admin_user_id: 1, created_at: “2015-09-20 18:37:29”, updated_at: “2015-09-20 18:37:29”, deal_available: true>] How can I update the object’s ‘deal_available’ attribute inside my database in raw postgresql ? I tried different ways to write it but I stumble […]

How to deal with array pointers in ruby Fiddle

I am trying to switch from Ruby FFI to Fiddle, which is now part of Ruby std lib. It is pretty undocumented though, and I am having a hard time in figuring out how to deal with arrays and pointers. In particular, how can I write a Fiddle interface to a C function like this: […]

Simple Ruby loop through array does not iterate completely

So I thought I understood this, but I’m not getting the output I expected, so obviously I don’t understand it. In Ruby (2.0.0) a = [1,2,3,4] a.each do |e| a.delete(e) end a = [2,4] It doesn’t seem to be looping through each item in the array. However, when I simply output the item, it loops […]

Ruby bug appears only when using print statements inside a block for array.each

If I call the function anagrams below in irb, I get a non-empty hash container as expected. But if you comment out the print “No Key\n” line, the returned hash container is now empty. In fact for all elements in list the code in the elsif branch seems to execute. Either I’m going nuts or […]

Return transformed nmatrix array with fftw3

I am creating a ruby wrapper for the fftw3 library for the Scientific Ruby Foundation which uses nmatrix objects instead of regular ruby arrays. I have a curious problem in returning the transformed array in that I am not sure how to do this so I can check the transform has been computed correctly against […]

Ruby is the best programming language in the world - Ruby on Rails.