Rubocop errors

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code inside a conditional expression.
     if turn.to_i == @num
     ^^

# piece of code
def a_winner?(turn)
   if turn.to_i == @num
     @try == 1 ? big_winner : winner
   end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
       (Show.warn_number; redo) unless a_number?(turn)
       ^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Rubocop is just a style analyst. Your code will work, it's just contradicting ruby style guide

- Thomas Perkins

···

On Nov 19, 2016, at 9:01 AM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code inside a conditional expression.
   if turn.to_i == @num
   ^^

# piece of code
def a_winner?(turn)
if turn.to_i == @num
   @try == 1 ? big_winner : winner
end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
     (Show.warn_number; redo) unless a_number?(turn)
     ^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Ok, i'm looking also for best practice.
Do you know a better code analyst ?

Thank you very much.

/Nathan

···

Le 2016-11-19 19:13, thomas Perkins a écrit :

Rubocop is just a style analyst. Your code will work, it's just
contradicting ruby style guide

- Thomas Perkins

On Nov 19, 2016, at 9:01 AM, Nathan Guilty <ruby@e-solutions.re> >> wrote:

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code inside a conditional expression.
   if turn.to_i == @num
   ^^

# piece of code
def a_winner?(turn)
if turn.to_i == @num
   @try == 1 ? big_winner : winner
end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
     (Show.warn_number; redo) unless a_number?(turn)
     ^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Rubocop is the best code analyst =)

···

On Nov 19, 2016 18:18, "Nathan Guilty" <ruby@e-solutions.re> wrote:

Ok, i'm looking also for best practice.
Do you know a better code analyst ?

Thank you very much.

/Nathan

Le 2016-11-19 19:13, thomas Perkins a écrit :

Rubocop is just a style analyst. Your code will work, it's just
contradicting ruby style guide

- Thomas Perkins

On Nov 19, 2016, at 9:01 AM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code
inside a conditional expression.
   if turn.to_i == @num
   ^^

# piece of code
def a_winner?(turn)
if turn.to_i == @num
   @try == 1 ? big_winner : winner
end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
     (Show.warn_number; redo) unless a_number?(turn)
     ^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

def a_winner?(turn)
  return unless turn.to_i == @num

  @try == 1 ? big_winner : winner
end

Thats what rubocop is suggesting. However, since you were asking for
general advice, there is some improvement you could make.
The "a_winner" method seems to be a little all over the place. Why do you
need a method to do this, why do you have to pass that argument, why would
a method with "?" return anything else but true or false?
I have no clue how your programm works, but consider writing specs (at
least in your head) for all steps in you programm, and make it as
expressive as possible before even writing your code... In RSpec you could
start out with something like:

...
  describe "game#roll_dice" do
    ...

    context "when dice are not equal"
      it "should determine a winner"
        player_1 = Player.new
        player_2 = Player.new
        game = Game.new(player_1, player_2)
        game.roll_dice
        # do something so your dice do not come out equal
        expect(game).to have_a_winner
      end
    end
  end

This would apply "a_winner?" to the game object, it does not take any
arguments but is super expressive and should only return true or false or
truthy or falsy.

Furthermore you should return all those @variable calls with a plain
method/variable call, it makes your program a lot more flexible and ruby
even provides you with "attr_accessor" and "attr_reader" to replace the
@variable calls...

def a_winner?
  return false unless turn.to_i == num # no clue what this does, but lets
keep it for the sake of it...
  big_winner || winner
end

Ruby gives you the chance to express what you actually want to do in code!
I wont say my code examples are the best, but as long as you can just be
like "ok, I read it and I understand whats supposed to be happening" you
did something right!

Happy coding :smiley:

···

On Sun, Nov 20, 2016 at 12:30 AM, Aleksey Ivanov <ialexxei@gmail.com> wrote:

Rubocop is the best code analyst =)

On Nov 19, 2016 18:18, "Nathan Guilty" <ruby@e-solutions.re> wrote:

Ok, i'm looking also for best practice.
Do you know a better code analyst ?

Thank you very much.

/Nathan

Le 2016-11-19 19:13, thomas Perkins a écrit :

Rubocop is just a style analyst. Your code will work, it's just
contradicting ruby style guide

- Thomas Perkins

On Nov 19, 2016, at 9:01 AM, Nathan Guilty <ruby@e-solutions.re> wrote:

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code
inside a conditional expression.
   if turn.to_i == @num
   ^^

# piece of code
def a_winner?(turn)
if turn.to_i == @num
   @try == 1 ? big_winner : winner
end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
     (Show.warn_number; redo) unless a_number?(turn)
     ^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Thank you very much for your explanations Fabian, i understand better.

/Nathan

def a_winner?(turn) return unless turn.to_i == @num
@try == 1 ? big_winner : winner

end

Thats what rubocop is suggesting. However, since you were asking for general advice, there is some improvement you could make.
The "a_winner" method seems to be a little all over the place. Why do you need a method to do this, why do you have to pass that argument, why would a method with "?" return anything else but true or false?
I have no clue how your programm works, but consider writing specs (at least in your head) for all steps in you programm, and make it as expressive as possible before even writing your code... In RSpec you could start out with something like:

...
describe "game#roll_dice" do
...

context "when dice are not equal"
it "should determine a winner"
player_1 = Player.new
player_2 = Player.new
game = Game.new(player_1, player_2)
game.roll_dice
# do something so your dice do not come out equal
expect(game).to have_a_winner
end
end
end
This would apply "a_winner?" to the game object, it does not take any arguments but is super expressive and should only return true or false or truthy or falsy.

Furthermore you should return all those @variable calls with a plain method/variable call, it makes your program a lot more flexible and ruby even provides you with "attr_accessor" and "attr_reader" to replace the @variable calls...

def a_winner?
return false unless turn.to_i == num # no clue what this does, but lets keep it for the sake of it...
big_winner || winner
end

Ruby gives you the chance to express what you actually want to do in code! I wont say my code examples are the best, but as long as you can just be like "ok, I read it and I understand whats supposed to be happening" you did something right!

Happy coding :smiley:

Rubocop is the best code analyst =)

Ok, i'm looking also for best practice.
Do you know a better code analyst ?

Thank you very much.

/Nathan

Rubocop is just a style analyst. Your code will work, it's just
contradicting ruby style guide

- Thomas Perkins

Hi,

I have the following issues(2) :

***ISSUE 1
lib/core.rb:83:5: C: Use a guard clause instead of wrapping the code inside a conditional expression.
if turn.to_i == @num
^^

# piece of code
def a_winner?(turn)
if turn.to_i == @num
@try == 1 ? big_winner : winner
end
end

Any idea ?

***ISSUE 2
lib/core.rb:95:7: C: Don't use parentheses around a method call.
(Show.warn_number; redo) unless a_number?(turn)
^^^^^^^^^^^^^^^^^^^^^^^^

Why it is a problem ?

Thank you very much for your help.

/Nathan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk [1]>

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk [1]>

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk [1]>

Unsubscribe:
<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Links:

···

Le 2016-11-19 21:06, Fabian Zitter a écrit :

On Sun, Nov 20, 2016 at 12:30 AM, Aleksey Ivanov <ialexxei@gmail.com> wrote:
On Nov 19, 2016 18:18, "Nathan Guilty" <ruby@e-solutions.re> wrote:
Le 2016-11-19 19:13, thomas Perkins a écrit :
On Nov 19, 2016, at 9:01 AM, Nathan Guilty <ruby@e-solutions.re> wrote:

------
[1] http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk