111
votes
9answers
6k views

Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)

I was going through the exercises at http://rubykoans.com/ and I was struck by the following Ruby quirk that I found really unexplainable: array = [:peanut, :butter, :and, :jelly] array[0] => ...
135
votes
9answers
60k views

How to sum array members in Ruby?

I have an array of integers. For example: array = [123,321,12389] Is there any nice way to get the sum of them? I know, that sum = 0 array.each { |a| sum+=a } would work.
33
votes
7answers
30k views

Create two-dimensional arrays and access sub-arrays in Ruby

I wonder if there's a possibility to create a two dimensional array and to quickly access any horizontal or vertical sub array in it? I believe we can access a horizontal sub array in the following ...
7
votes
3answers
471 views

Ruby method Array#<< not updating the array in hash

Inspired by http://stackoverflow.com/questions/2552363/how-can-i-marshal-a-hash-with-arrays I wonder what's the reason that Array#<< won't work properly in the following code: h = ...
26
votes
6answers
18k views

count duplicate elements in ruby array

I have an sorted array like this: ['FATAL <error title="Request timed out.">', 'FATAL <error title="Request timed out.">', 'FATAL <error title="There is insufficient system memory to ...
7
votes
9answers
5k views

How to count duplicates in Ruby Arrays

How do you count duplicates in a ruby array? For example, if my array had three a's, how could I count that
9
votes
2answers
2k views

Need to split arrays to sub arrays of specified size in Ruby

I have an array that is something like this: arr = [4, 5, 6, 7, 8, 4, 45, 11] I want a fancy method like sub_arrays = split (arr, 3) This should return the following: [[4, 5, 6], [7,8,4], ...
146
votes
4answers
40k views

Ruby: what does %w(array) mean?

I'm looking at the documentation for FileUtils. I'm confused by the following line: FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6' What does the %w mean? Can you point me to the ...
118
votes
2answers
34k views

How do I pick randomly from an array?

I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this: myArray = ["stuff", ...
180
votes
7answers
66k views

check if value exists in array in Ruby

If I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird'], how do I check this w/o looping through. Is there a simple way of checking if the value exists, nothing more?
19
votes
11answers
10k views

Uniq by object attribute in Ruby

What's the most elegant way to select out objects in an array that are unique with respect to one or more attributes? These objects are stored in ActiveRecord so using AR's methods would be fine too. ...
14
votes
3answers
6k views

Where is it legal to use ruby splat operator?

Splats are cool. They're not just for exploding arrays, although that is fun. They can also cast to Array and flatten arrays (See http://github.com/mischa/splat/tree/master for an exhaustive list of ...
13
votes
2answers
1k views

Why does array.slice behave differently for (length, n)

If I have an array a: a[a.length] returns nil. Good. a[a.length, x] returns []. Good. a[a.length+x, y] returns nil. Inconsistent with 2. While this behavior is documented, it seems odd. Can ...
2
votes
5answers
328 views

Remove from the array elements that are repeated

What is the best way to remove from the array elements that are repeated. For example, from the array a = [4, 3, 3, 1, 6, 6] need to get a = [4, 1] My method works to too slowly with big amount ...
21
votes
7answers
21k views

Understanding ruby array sort

I am having a problem understanding how array.sort{|x,y| block} works exactly,hence how to use it? ruby-doc example: a = [ "d", "a", "e", "c", "b" ] a.sort #=> ["a", "b", ...

1 2 3 4 5 12
15 30 50 per page