Array#rotate equivalent in ruby 1.8.7
If you require 'backports/1.9.2/array/rotate'
, you will get Array#rotate
and rotate!
in older versions of Ruby.
Either way, you avoid reinventing the wheel, and more importantly you gain the advantage of an implementation that passes RubySpec. It will work for all corner cases and ensure compatibility with Ruby 1.9.
For example, none of the two answers given work for []
!
Array#rotate produces a NoMethod error
It's new to Ruby 1.9. You must be using Ruby 1.8.6 or 1.8.7.
Here is what you can do.
Printing an ASCII spinning cursor in the console
Yes, this works on Windows, OS X, and Linux. Improving on Niklas' suggestion, you can make this more general like so:
def show_wait_cursor(seconds,fps=10)
chars = %w[| / - \\]
delay = 1.0/fps
(seconds*fps).round.times{ |i|
print chars[i % chars.length]
sleep delay
print "\b"
}
end
show_wait_cursor(3)
If you don't know how long the process will take, you can do this in another thread:
def show_wait_spinner(fps=10)
chars = %w[| / - \\]
delay = 1.0/fps
iter = 0
spinner = Thread.new do
while iter do # Keep spinning until told otherwise
print chars[(iter+=1) % chars.length]
sleep delay
print "\b"
end
end
yield.tap{ # After yielding to the block, save the return value
iter = false # Tell the thread to exit, cleaning up after itself…
spinner.join # …and wait for it to do so.
} # Use the block's return value as the method's
end
print "Doing something tricky..."
show_wait_spinner{
sleep rand(4)+2 # Simulate a task taking an unknown amount of time
}
puts "Done!"
This one outputs:
Doing something tricky...|
Doing something tricky.../
Doing something tricky...-
Doing something tricky...\
(et cetera)
Doing something tricky...done!
Ruby method to cycle array to the right
You can use rotate
method which does similar things
2.6.5 :012 > arr = [1,2,3,4]
=> [1, 2, 3, 4]
2.6.5 :013 > arr.rotate
=> [2, 3, 4, 1]
2.6.5 :014 > arr = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
2.6.5 :015 > arr.rotate(2)
=> [3, 4, 5, 1, 2]
For Ex :-
2.6.5 :021 > def array_rotation(array, shift = 1)
2.6.5 :022?> array.rotate(- shift)
2.6.5 :023?> end
=> :array_rotation
2.6.5 :024 > arr = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
2.6.5 :025 > array_rotation arr, 2
=> [4, 5, 1, 2, 3]
2.6.5 :026 > arr = [1,2,3,4]
=> [1, 2, 3, 4]
2.6.5 :027 > array_rotation arr, 1
=> [4, 1, 2, 3]
How do I iterate through an array and change characters efficiently?
I like the tr
approach, but if you want something similar to next
, then Array#rotate
could be a good option; here's an example:
def letter_swap(full_name)
vowels = %w(a e i o u)
consonants = %w(b c d f g h j k l m n p q r s t v w x y z)
full_name.each_char.with_object("") do |char, new_name|
if vowels.include?(char)
new_name << vowels.rotate(vowels.index(char) + 1).first
elsif consonants.include?(char)
new_name << consonants.rotate(consonants.index(char) + 1).first
else
new_name << char
end
end
end
Of course you could DRY this code, but at the expense of readability (IMO); for example:
def letter_swap(full_name)
letters = [%w(a e i o u), %w(b c d f g h j k l m n p q r s t v w x y z)]
full_name.each_char.with_object("") do |char, new_name|
group = letters.find { |group| group.include?(char) }
new_name << (group.nil? ? char : group.rotate(group.index(char) + 1).first)
end
end
How do I find the first two consecutive elements in my array of numbers?
You could use each_cons
and find
to get the first element from the array of pairs where the second element less the first one is equal to 1:
p [1, 7, 8, 12, 14, 15].each_cons(2).find { |a, b| b - a == 1 }
# => [7, 8]
How do you rotate a two dimensional array?
Here it is in C#
int[,] array = new int[4,4] {
{ 1,2,3,4 },
{ 5,6,7,8 },
{ 9,0,1,2 },
{ 3,4,5,6 }
};
int[,] rotated = RotateMatrix(array, 4);
static int[,] RotateMatrix(int[,] matrix, int n) {
int[,] ret = new int[n, n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
ret[i, j] = matrix[n - j - 1, i];
}
}
return ret;
}
Related Topics
Losing an Attribute When Saving Through an Association W/ Scope (Rails 4.0.0)
When Passing Objects as Parameters, Are They Passed by Reference
Rake Test Very Slow in Windows
Unexpected Keyword_End, Expecting $End (Syntaxerror)
When to Use Association Extensions VS Named Scopes
Problem Installing Ruby 1.9.2 on MAC Os Lion
What Are the Meanings of the Hash Keys When Calling Objectspace.Count_Objects
Error When Installing Libv8 3.11.8.3
How to Make Rake Db:Migrate Generate Schema.Rb When Using :SQL Schema Format
Ruby: Automatically Set Instance Variable as Method Argument
Svg Files in Raphael, Can They Be Used
What Does a Single Splat/Asterisk in a Ruby Argument List Mean
Ruby Spreadsheet Row Background Color
Https Request Using Net::Http's Block Form -- Is It Possible
Ruby 'Range.Last' Does Not Give the Last Value. Why
Setting a Global Within a Proc