How to convert Fixnum to Integer in ruby - ruby

I am trying to get Integers, but i am getting 'Fixnum' values.
For Eg:
arr = ["1", "2", "3", "4"]
arr.each do |a|
m = a.to_i
m.class.name
Result
=> Fixnum
According to the above example, how can i get Integer values?
Fixnum is a Integer only but while implementing one plugin, it will through an error like 'Please enter only integer'.

In Ruby integers are either of the class Fixnum or Bignum for bigger numbers. Both of them inherit from the Integer class.
So you already got an integer, no need to convert it further.
1.class #=> Fixnum
1.class.superclass #=> Integer
To convert the array elements to integers you would do this:
arr = ["1", "2", "3", "4"]
arr.map(&:to_i) #=> [1, 2, 3, 4]

Fixnum is the ruby class for standard integers.
Well to be specific, the Integer class covers both Fixnums and Bignums, but in all honesty there's nothing to done here.

All Fixnum(s) are already Integer. Here is some sample:
"12".to_i.class
#=> Fixnum
"12".to_i.integer?
#=> true
"12".to_i.to_int
#=> 12
The above all is possible as-
"12".to_i.class.superclass
#=> Integer

Related

Combinations of consecutive elements of an array

Is there any builtin method to produce combinations of consecutive array elements?
a = ['1','2','3','4']
# => '12','23','34'
I tried the methods permutation, combination, and each_slice, but was not able to produce required output.
a.permutation(2).to_a #=> [[1,2],[1,3],[1,4],[2,1],[2,3],[2,4],[3,1],[3,2],[3,4]]
a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
a.each_slice(2) {|a| p a} #=> ["1", "2"],["3", "4"]
No, but you can do it with a combination of a few methods.
a.each_cons(2).map(&:join)
# => ["12", "23", "34"]

Ruby element match

I am trying to find the index of the first and second instance of a string variable. I want to be able to use any predefined string variable but when I try to do that it gives me an error. I want to be able to declare multiple string variables like ss, aa, ff, etc and use them in place of xx. Can someone help me out?
#aa is a predefined array
xx = "--help--"
find_xx_instance = aa.each_with_index.select{|i,idx| i =~ /xx/}
#/--help--/works but not /xx/
find_xx_instance.map! {|i| i[1]}
#gives me info between the first two instances of string
puts aa[find_xx_instance[0]+1..find_xx_instance[1]-1]
As far as I understand, you just need to pass variable to regular expression. Try this:
find_xx_instance = aa.each_with_index.select{|i,idx| i =~ /#{xx}/}
I have assumed you are given an array of strings, arr, a string str, and an integer n, and wish to return an array a of n elements i, where i is the index of the ith+1 instance of str in arr.
For example:
arr = %w| Now is the time for the Zorgs to attack the Borgs |
#=> ["Now", "is", "the", "time", "for", "the", "Zorgs", "to", "attack", "the", "Borgs"]
str = "the"
nbr = 2
This is one way:
b = arr.each_index.select { |i| arr[i]==str }
#=> [2, 5, 9]
b.first(nbr)
#=> [2, 5]
which can be written
arr.each_index.select { |i| arr[i]==str }.first(nbr)
This For small problems like this one, that's fine, but if arr is large, it would be better to terminate the calculations after nbr instances of str have been found. We can do that by creating a Lazy enumerator:
arr.each_index.lazy.select { |i| arr[i]==str }.first(nbr)
#=> [2, 5]
Here's a second example that clearly illustrates that lazy is stopping the calculations after nbr strings str in arr have been found:
(0..Float::INFINITY).lazy.select { |i| arr[i] == str }.first(nbr)
#=> [2, 5]

Simple ruby array smallest integer?

This is my array : array = ["1", "Hel", "6", "3", "lo" ]I want to output the smallest number in the array. Then I want to output the largest number in the array? How do I achieve this? Thanks!
Well it depends how you want to handle the string elements that aren't easily parsed into numbers. Like "Hel" and "lo".
If you do this:
array.map {|x| Integer(x) rescue nil }.compact.min
array.map {|x| Integer(x) rescue nil }.compact.max
Then you'll ignore those, which is probably the right thing, assuming you don't have some reason for considering "Hel" and "lo" to have numerical values.
numbers = array.select { |x| x[/^-?\d+$/] }.map(&:to_i)
# => [1, 6, 3]
numbers.min
# => 1
numbers.max
# => 6
Another variation to work with negative numbers
smalles, largest =
["1", "Hel", "6", "3", "lo","-9" ].select { |x| x[/^-?\d+$/] }.minmax_by(&:to_i)
smallest # => -9 largest # => 6
smallest, largest =
["1", "Hel", "6", "3", "lo" ].reject{|s| s =~ /\D/}.minmax_by(&:to_i)
smallest # => "1"
largest # => "6"
Another way:
array.join(',').scan(/-?\d+/).minmax_by(&:to_i)
#=> ["-4", "6"]
we can use unicode [[:digit:]] instead writing regular expression as
array.join(',').scan(/[[:digit:]]/).minmax_by(&:to_i)

Array of Strings to Convert to Mixed Array

I'm trying to convert an Array of Arrays consisting of Ruby Strings into an Array of Arrays consisting of Strings and Floats.
Here is my attempt:
array = [["My", "2"], ["Cute"], ["Dog", "4"]]
array.collect! do |x|
x.each do |y|
if y.gsub!(/\d+/){|s|s.to_f}
end
end
end
=> [["My", "2.0"], ["Cute"], ["Dog", "4.0"]]
I'm looking for this to rather return [["My", 2.0], ["Cute"], ["Dog", 4.0]] What did I do wrong?
What you did wrong is that you used gsub!. That takes a string and changes the string. It doesn't turn it into anything else, no matter what you do (even if you convert it to a float in the middle).
A simple way to achieve what you want is:
[["My", "2"], ["Cute"], ["Dog", "4"]].map{|s1, s2| [s1, *(s2.to_f if s2)]}
If you do not want to create the element array, but replace its contents, then:
[["My", "2"], ["Cute"], ["Dog", "4"]].each{|a| a[1] = a[1].to_f if a[1]}
If the numerical strings appear in random positions, then:
[["My", "2"], ["Cute"], ["Dog", "4"]]
.each{|a| a.each.with_index{|e, i| a[i] = a[i].to_f if a[i] and a[i] =~ /\d+/}}

How do I pass an array to a method that accepts an attribute with a splat operator?

If I have a method like:
def sum *numbers
numbers.inject{|sum, number| sum += number}
end
How would I be able to pass an array as numbers?
ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6
ruby-1.9.2-p180 :045 > sum([1,2,3]) #=> [1, 2, 3]
Note that I can't change the sum method to accept an array.
Just put a splat when calling the method?
sum(*[1,2,3])
Did you mean this?
sum(*[1,2,3])
#Dogbert was first

Resources