简体   繁体   中英

Ruby operators - formula

I'm trying to make a formula in my project.rb model in my rails 4 app.

I have an attribute in an preference table, called delay. I want to calculate whether the tolerance on one user's part, is close to the delay required by another user.

In my project.rb, I've tried to do this as follows:

def publication_delay_variance
    if @current_user.profile.organisation.preference.delay >=  @project.profile.organisation.preference.delay
      'No problems here'
    elsif @current_user.profile.organisation.preference.delay * 90% >= @project.profile.organisation.preference.delay
      "Close, but not quite there"
    else   @current_user.profile.organisation.preference.delay * 50% >=  @project.profile.organisation.preference.delay

      "We're not in alignment here"
    end
  end

the current user is the current user who is currently logged in and interacting with the page. The other user is the user who created the project. Each user has an organisation. Each organisation has preferences. I'm trying to compare them.

Can anyone see what I'm doing wrong? I don't have much experience with this. My current attempt generates this error:

syntax error, unexpected >=
...ence.publication_delay * 90% >= @project.profile.organisatio...
..

The problem is that 90% isn't valid in Ruby. You probably meant to use 0.9 instead. Also, your last else should be an elsif :

def publication_delay_variance
  if @current_user.profile.organisation.preference.delay >= @project.profile.organisation.preference.delay
    'No problems here'
  elsif @current_user.profile.organisation.preference.delay * 0.9 >= @project.profile.organisation.preference.delay
    "Close, but not quite there"
  elsif @current_user.profile.organisation.preference.delay * 0.5 >= @project.profile.organisation.preference.delay
    "We're not in alignment here"
  end
end

Of course, without an else you don't have a default case, so you should consider what behavior you want if none of those three conditions is true .

PS You can make this a lot more readable by assigning those values to local variables with shorter names:

def publication_delay_variance
  user_delay = @current_user.profile.organisation.preference.delay
  project_delay = @project.profile.organisation.preference.delay

  if user_delay >= project_delay
    "No problems here"
  elsif user_delay * 0.9 >= project_delay
    "Close, but not quite there"
  elsif user_delay * 0.5 >= project_delay
    "We're not in alignment here"
  end
end

PPS 0.9 and 0.5 are magic numbers . Consider moving their values into constants.

In Ruby, % is the modulus operator, which takes two arguments x % y and returns the remainder of x / y. It doesn't make sense to have >= immediately after it, which is what the error message is telling you. To represent a percentage in Ruby, use a decimal number, eg 0.9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM