0

In the upvotes= method shown below, you'll find an alternate syntax for the 'if' statement. How would I write code that adds 5 to @upvotes unless the story is about "business"?

def upvotes=(upvotes)
@upvotes = upvotes
@upvotes *= 5 if is_about?("art")
@upvotes *= 8 if is_about?("music")
@upvotes *= 2 if is_about?("entertainment")

I am trying to understand how I would write this. Everything I have tried isn't working.

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
cmoney
  • 19
  • 3
  • Have a look at the `unless` statement and the [+=](http://stackoverflow.com/questions/7638502/what-does-plus-equals-mean) operator. – user2398029 Feb 24 '14 at 19:45
  • It is very confusing to understand.. Give some more context. Or provide some more samples to mirror your need, and the expected output also – Arup Rakshit Feb 24 '14 at 19:58

1 Answers1

0

Exactly how you say. Something like:

@upvotes += 5 unless is_about?("business")
Abdo
  • 13,549
  • 10
  • 79
  • 98
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • @cmoney that depends on the behavior you want... do you want this to be the only condition that increases upvotes? Or do you want to keep the other variations as well (multiplied by 5 if it's 'art', 8 if it's 'music', '2' if it's entertainment). If you don't need those, then you can remove them. – nzifnab Feb 24 '14 at 19:49
  • So i would just include that under the def upvotes? Or would i need to unclude "music" "entertainment" all along with it? – cmoney Feb 24 '14 at 19:50