Turning long fixed number to array Ruby
You don't need to take a round trip through string-land for this sort of thing:
def digits(n)
Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
end
ary = digits(74239)
# [7, 4, 2, 3, 9]
This does assume that n
is positive of course, slipping an n = n.abs
into the mix can take care of that if needed. If you need to cover non-positive values, then:
def digits(n)
return [0] if(n == 0)
if(n < 0)
neg = true
n = n.abs
end
a = Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
a[0] *= -1 if(neg)
a
end
How do I iterate through the digits of an integer?
The shortest solution probably is:
1234.to_s.chars.map(&:to_i)
#=> [1, 2, 3, 4]
A more orthodox mathematical approach:
class Integer
def digits(base: 10)
quotient, remainder = divmod(base)
quotient == 0 ? [remainder] : [*quotient.digits(base: base), remainder]
end
end
0.digits #=> [0]
1234.digits #=> [1, 2, 3, 4]
0x3f.digits(base: 16) #=> [3, 15]
Ruby Convert integer to binary to integer array of set bits
This would work:
i = 98
(0...i.bit_length).map { |n| i[n] << n }.reject(&:zero?)
#=> [2, 32, 64]
Fixnum#bit_length
returns the position of the highest "1" bitFixnum#[n]
returns the integer's nth bit, i.e.0
or1
Fixnum#<<
shifts the bit to the left.1 << n
is equivalent to 2n
Step by step:
(0...i.bit_length).map { |n| i[n] }
#=> [0, 1, 0, 0, 0, 1, 1]
(0...i.bit_length).map { |n| i[n] << n }
#=> [0, 2, 0, 0, 0, 32, 64]
(0...i.bit_length).map { |n| i[n] << n }.reject(&:zero?)
#=> [2, 32, 64]
You might want to reverse
the result.
How to convert an array of string integers to an array of integers? Ruby
all_ages = ["11", "9", "10", "8", "9"]
Code
p all_ages.map(&:to_i).sum
Or
p all_ages.map { |x| x.to_i }.sum
Output
47
Convert string of numbers to array of numbers
str.split(",").map {|i| i.to_i}
but the idea is same to you....
Create array in Ruby with begin value, end value and step for float values
For floats with custom stepping you can use Numeric#step like so:
-1.25.step(by: 0.5, to: 1.25).to_a
# => [-1.25, -0.75, -0.25, 0.25, 0.75, 1.25]
If you are looking on how to do this with integer values only, see this post or that post on how to create ranges and simply call .to_a
at the end. Example:
(-1..1).step(0.5).to_a
# => [-1.0, -0.5, 0.0, 0.5, 1.0]
How to get the sum an array of strings in ruby
You just need to do
array.map(&:to_f).reduce(:+)
Explanation :-
# it give you back all the Float instances from String instances
array.map(&:to_f)
# same as
array.map { |string| string.to_f }
array.map(&:to_f).reduce(:+)
# is a shorthand of
array.map(&:to_f).reduce { |sum, float| sum + float }
Documentation of #reduce
and #map
.
Related Topics
Best Practices For Reusing Code Between Controllers in Ruby on Rails
Unresolved Specs During Gem::Specification.Reset:
Ruby on Rails Best Practices - Big Controller VS Small Controller
Convert Time from One Time Zone to Another in Rails
Using Send_File to Download a File from Amazon S3
Gem Install Permission Problem
How to Use Activerecord in a Ruby Script Outside Rails
How to Wrap Link_To Around Some HTML Ruby Code
How to Use Bundler Behind a Proxy
Rails4 Unknown Encoding Name - Cp720
What Is the Use of Gemfile in Rails
How to Use Global Variables or Constant Values in Ruby
What Are the Benefits of the New Hash Syntax in Ruby 1.9
What Is Java Interface Equivalent in Ruby
How to Remove a Key from Hash and Get the Remaining Hash in Ruby/Rails