Is an integer variable not numeric in Ruby? - ruby-on-rails

I have user_waiting period saved as an integer in my DB. I am trying to use it in the below code, but I keep getting the error "Expected Numeric." I am very new to Ruby, and my understanding was that an integer would be numeric. Am I incorrect in this understanding?
if #pto_management.waiting_period.nil?
#pto_management.accrual_begin_date = current_user.start_date
else
#pto_management.accrual_begin_date = current_user.start_date +
#pto_management.user_waiting_period
end

Perhaps explicitly convert it to an int?
if #pto_management.waiting_period.nil?
#pto_management.accrual_begin_date = current_user.start_date
else
#pto_management.accrual_begin_date = current_user.start_date +
#pto_management.user_waiting_period.to_i # <-- HERE
end

Related

How to add(increment) integer value like we do in other languages using ++(increment) operator in Ruby on Rails

I'm trying to add to integer value + 1 , but I get 'no implicit conversion of Integer into Array'
Here is my code:
#user_id = User.limit(1).order('created_at desc').pluck(:user_id).map(&:to_i)
#user_id = #user_id + 1
'no implicit conversion of Integer into Array'
It's because .pluck() and .map() return an array.
#user_id + 1 => is trying to add an array(#user_id) and an integer(1).
Since 1 is an integer which should be cast to an array to get added to an array and ruby doesn't want to do it implicitly.
Hence the error.
Why not this?
User.limit(1).order('created_at desc') will return last active record. If created_at is not tempered you can achieve the same simply by
#user_id = User.last.user_id + 1
.pluck and .map both returns an array type, so you're trying to do a numeric addition to array type, so you can do
#user_id = #user_id.first + 1
You can call + on array but it needs to be between two array types, e.g.
[1,2] + [3,4] => [1,2,3,4]
Hope this helps.

I want the short the person based on the post,but it is showing an error that, `[]': no implicit conversion of String into Integer (TypeError)

I want the short the person based on the post,but it is showing an error that, `[]': no implicit conversion of String into Integer (TypeError)
merchant = Hash.new
BusinessOwner.each{|owner|
if !owner.phoneNumber.blank? && !owner.phoneNumber.phoneNumber.blank? && !owner.phoneNumber.phoneNumber.include?("-reset")
merchant["name"] = owner.name
merchant["phone"] = owner.phoneNumber.countryCode.to_s + owner.phoneNumber.phoneNumber.to_s
merchant["post"] = owner.account.items.count
puts merchant
end
}
result = merchant.sort_by{|k| k['post']}.reverse
result.first

How to set a variable to nil if the user leaves their answer as blank?

I'm learning ruby and am a bit stuck. They want us to set a variable as nil if the user leaves the question blank. Otherwise convert their answer to an integer. I came up with the following, but when I leave the answer blank, it prints 0. Could you steer me in the right direction?
puts "What's your favorite number?"
number = gets.chomp
if number == ' '
number = nil
else
number = number.to_i
end
p number
You're only testing if the entered number is explicitly a single space. If you're testing for 'blankness' you probably want to strip the input you receive and then test if it is empty?.
E.g.
number = gets.strip
if number.empty?
number = nil
else
number = number.to_i
end
You've tagged this with ruby-on-rails so I'm assuming you are considering a string to be blank if blank? returns true (i.e. the string is empty or consists only of whitespace. If you are using rails then you can use that blank? method to test the input:
number = gets
if number.blank?
number = nil
else
number = number.to_i
end
You've got an extra space - if number == ' ' - this should be if number == ''
An alternative way would be to say if number.length == 0
you can do like this
if number.empty?
number = nil
else
number = number.to_I
end
or in single line you can do like this
number = number.empty? ? nil : number.to_i

Making a new array considered a Dynamic Assignment Ruby

I want to make an array where to keep scores for every user. An example of the array would be ScoreArray["example#yahoo.com"] = 10. To do this I need to instantiate the Array first. So I tried something like:
ScoreArray = Array.new
#sugested.each do |gg|
nr = 0
#followees.each do |ff|
if (ff.email == gg.email) then nr = nr + 1
end
end
if(gg.following.count != 0) then
score = ( nr/#followees.count ) * ( gg.followers.count / gg.following.count)
ScoreArray[gg.email] = score
pry.bind
else score = 0
end
end
All this code is inside a method called candidates . When I try to run rails server I get the following error message on the page where I invoke this method :
home/flo/Ruby/Licenta/honk_app/app/controllers/application_controller.rb:45: dynamic constant assignment ScoreArray = Array.new ^
Any ideas how can I avoid this problem? And why is it doing this?(from what I've read is because it's inside a method and ruby doesn't like instantiating a "constant" each time a method is called. The thing is , this is not a constant ... for each user that logs in I will have a separate array).
In ruby a leading capital letter denotes a constant - if you don't want a constant then start with a lowercase letter (if a local variable isn't sufficient for your purpose, consider an instance variable)
In addition arrays can't be used as you show them
some_array[gg.email]
Will raise an exception if gg.email is a string
Try this
score_hash = Hash.new
score_hash[gg.email] = score
sorted_hash = Hash[score_hash.sort_by{|k, v| v}.reverse]

Simplifying an expression using .map

Below I have an example active record query using a bunch of each iterators
user.user_spells.each do |us|
us.spell.buff_effects.where(stat_effected:'gold').each do |be|
value = value + be.value
end
end
I would like to use .map to return a list of all the results so I can do it essentially in one line.
I came up with:
user.user_spells.map { |us| us.spell.buff_effects.where(stat_effected:stat_effected) }.each do |be|
value = value + be.value
end
However... the .map block returns some empty arrays. Not sure how to write it correctly.
Any help would be appreciated! Thanks
Probably along these lines, if what you want is the sum of values in the end:
value =
user.user_spells.flat_map do |us|
us.spell.buff_effects.where(stat_effected:'gold').map(&:value)
end.reduce(&:+)

Resources