0
votes
1answer
21 views
How do I create a subset of an array based on an array of indexes for that array in Ruby
If I have an array like this:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
and I want to select a subset of that array based on this arbitrary array of indexes:
[0,1,4,7,8,13,14,15,18,19]
...
-2
votes
2answers
50 views
Ruby Hashing - Multiple Keys [closed]
I have a Ruby array which I get from the database which looks like this when I print it :
<% @Aarr.each do |row1| %>
<%= row1[0] %> : <%= row1[1] %> : <%= row1[2] %>
...
1
vote
2answers
21 views
Check if element in array was created_at within last 5 seconds
I have an array @user.photos which will return something like:
[#<Photo id: 53, photo_file_name: "Freedom-Tower",
photo_content_type: "image/jpeg", photo_file_size: 1937702,
photo_updated_at: ...
1
vote
1answer
31 views
change contents of shallow copied array of strings in ruby
Suppose I create the following arrays in ruby:
a = ["apple", "cherry"]
b = a.dup
Here, b is a shallow copy of a. So if I do:
a.each{|fruit| fruit << " pie"}
I get both a and b equal to ...
1
vote
2answers
51 views
Populate an array in ruby with 7n +1
How do I make the following array programatically in Ruby (1.9).
It follows the pattern 7n + 1 and I'd like it to contain 24 numbers.
arr = ["8","15","22","29","36","43","50","57","64","71" ]
1
vote
1answer
34 views
Sorting multidimensional array, descending
I have an multidimensional array in my ruby which looks like this :
arr= [{"10.0.1.50", "4"},
{"10.0.1.51", "10"},
{"10.0.1.48", "7"}]
I want to sort it such that the result should be:
...
-1
votes
2answers
60 views
Compare pattern of values in two arrays more efficiently
I need to test two arrays for equality each containing 8 items which are integers 1..7. The catch is that it's not the values per se I care about but the pattern of values. So for instance:
eq? [ ...
0
votes
1answer
25 views
Ruby: Shorthand for associative hash/array from mysql2 result set
So, new to ruby and struggling on this:
images table - 2 columns, filename and md5
using the mysql2 extension
images = Hash.new
results = client.query("SELECT * FROM images").each do |row|
...
0
votes
1answer
46 views
Find intersection of arrays
I'm having trouble making a simple form that finds the intersection of two arrays. The end goal is to find the intersection of two arrays of emails, but right now i'm simply testing with integers. ...
1
vote
2answers
62 views
Combine hashes with identical symbol values
array =
[ {
:keyword => "A",
:total_value => "10"
},
{
:keyword => "B",
:total_value => "5"
},
{
:keyword => "C",
...
2
votes
5answers
85 views
Reposition an element in an Array
Say I have...
arr = ["a", "b", "c"]
...and I want to move "a" between "b" and "c".
I currently do
arr.delete("a")
arr.insert(2, "a")
Can I do that in a single operation?
Thanks
1
vote
1answer
36 views
How to create a matrix with supplied dimensions or create dynamic variable name in Ruby for each subsequent array?
I'd like to create a class and pass 2 variables to that class; width(x) and length(y). With those variables the program should establish either a matrix or create x number of arrays with y number of ...
-1
votes
0answers
26 views
Consolidating duplicate openstruct values in array
I have an array of OpenStructs...
results=[
#<OpenStruct keyword="keyword_A",
total_value="50">,
#<OpenStruct keyword="keyword_B",
total_value="45">,
...
0
votes
3answers
79 views
Consolidating duplicate array items
I have an array of hashes...
array = [
{
'keyword' => 'A',
'total_value' => 50
},
{
'keyword' => 'B',
'total_value' => 25
},
{
'keyword' => 'C',
'total_value' ...
4
votes
1answer
227 views
Order a hash array by an array key
I am trying to filter and reorder an array of hashes. The filter and the order is defined by another array of strings, which represent the value of the "slug" key of the hash. The resulting array ...