How solve TypeError nil can't be coerced into Fixnum
Do you want to count the number of pairs in your set that are separated by K?
Your code works when you input 3 1
followed by 1 2 3
. It answers 2
.
- First, you really should describe a bit more what your goal is.
- Then, no need to input N. It just should be the size of your set.
- Write an example of the desired input before calling
gets
Here's a possible implementation :
require 'set'
puts 'Please type the integers of your set, separated by a space. Example : 1 2 3'
numbers = Set.new(gets.split.map{ |v| v.to_i})
# N=numbers.size # But you don't need it
puts 'Which pair difference are you looking for? Example : 1'
k = gets.to_i
pairs = numbers.select{|value| numbers.include?(value+k)}
count = pairs.size
puts "#{count} pair(s) found :"
pairs.each{|first_value|
puts format("(%d, %d)",first_value,first_value+k)
}
# Please type the integers of your set, separated by a space. Example : 1 2 3
# 1 2 3
# Which pair difference are you looking for? Example : 1
# 1
# 2 pair(s) found :
# (1, 2)
# (2, 3)
Ruby: nil can't be coerced into Fixnum (TypeError)
idx2
could overflow capacity of your array:
Imagine. nums = [1,2,3]
, so nums.length
is 3, idx1 = 1
, idx2 = 2
idx2 += 1 # ok now idx2 is 3
if idx2 == nums.length # ok true, idx2 == 3
idx1 += 1 # mmm, cool idx1 now 2
idx2 = idx1 + 1 # idx2 is 3
end
So in next iteration you will call
nums[idx2]
# same as
nums[3]
# ERROR! there is only 3 numbers in nums
And try to understand this code
def two_sums(nums)
nums[0..-2].each.with_index do |n,i|
nums[i+1..-1].each.with_index do |m,j|
return [i, i+j+1] if m + n == 0
end
end
nil
end
ERROR: Nil Can't be Coerced into a Fixnum
This suggests that one of your packs has an amount field which has not yet been set, so is nil
. When you try and add it to something else, it undergoes Type coercion, to see if Ruby can massage its type into one that can be added to numbers, but it can't, and so you have this error.
One solution is this:
def total_amount_spent_cents
packs.map(&:amount).compact.sum
end
Array#compact
removes the nil
elements.
This may be fixing the symptom and not the actual problem though. It could be the case that you shouldn't have nil
's in there at all, in which case you should check the initialisation of your Pack
model (or perhaps its validations, to ensure that amount
is mandatory).
`+' nil can't be coerced into FixNum (TypeError) won't go away
Your syntax is fine. The problem is that your methods are not validating data.
a += b
expands to a = a + b
, which will throw the error you're facing when b
is nil
.
You need to make sure that getQuote
always returns a number, or cast whatever you want to average into a number before performing mathematical operations.
For the former, you can change return orderbook["last"]
to return orderbook["last"].to_f
For the latter, sum += $averageArr[$averageArr.count - i].to_f
`*': String can't be coerced into Fixnum (TypeError)
I think you wanted something like this
space = " "
puts "Some Text: "
count = gets.chomp.to_i
puts space * count
puts "*"
and use the form string * fixnum
to repeat a string count
times
in `+': nil can't be coerced into Integer (TypeError) Ruby on Rails
You're checking up to the last element and then 2 number after it. Accessing an element in array bigger then the length returns nil
. Trying to add nil
to a number will produce the error you saw.
To solve it you should stop checking when the last of the 3 elements reaches the end of the array, not the first. Meaning you should stop 2 elements earlier.
It's easily achieved by just subtracting 2 from the length in your loop.
def lucky_sevens?(numbers)
i=0
while i < numbers.length - 2
if (numbers[i] + numbers[i+1] + numbers[i+2]) == 7
return true
end
i+=1
end
false
end
A more ruby way to do this, would be the following:
def lucky_sevens?(numbers)
numbers.each_cons(3).any? do |group_of_3|
group_of_3.sum == 7
end
end
Related Topics
Install Cocoapods Failed on MAC
Can't Convert String into Integer in Ruby/Ruby-On-Rails
How to Format a Date to Mm/Dd/Yyyy in Ruby
What Order Do Before Filters Occur In
In Ruby, What Are the Vertical Lines
Sharing an Enumerator Across Threads
Generate Activerecord Schema from an Existing Table
How to More Elegantly Remove Duplicate Items Across All Elements of a Ruby Array
Why Is This Not a Syntax Error
Difference Between Lambda and -> Operator in Ruby
When Do You Need a Require in a Rails Gemfile