Skip to main content
titles + step
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

No need for @sorted variable

You can remove the @sorted variable and return directly if no swaps were performed, change the loop to while true and add return if swaps == 0 instead of the if swaps ... @sorted =... statements.

No need for a class

You also do not need a class for this, you can either write a top-level function or put it into a module, a class is used to hold data and functions to operate on it, a class that holds a single static function is unreasonable.

Ruby step method

You should use the .step(2) method as it is more explicit than manual incrementing and modyfing a loop variable inside the body is unusual and breaks the expectation that we already know how many times a for loop should run.

You can remove the @sorted variable and return directly if no swaps were performed, change the loop to while true and add return if swaps == 0 instead of the if swaps ... @sorted =... statements.

You also do not need a class for this, you can either write a top-level function or put it into a module, a class is used to hold data and functions to operate on it, a class that holds a single static function is unreasonable.

No need for @sorted variable

You can remove the @sorted variable and return directly if no swaps were performed, change the loop to while true and add return if swaps == 0 instead of the if swaps ... @sorted =... statements.

No need for a class

You also do not need a class for this, you can either write a top-level function or put it into a module, a class is used to hold data and functions to operate on it, a class that holds a single static function is unreasonable.

Ruby step method

You should use the .step(2) method as it is more explicit than manual incrementing and modyfing a loop variable inside the body is unusual and breaks the expectation that we already know how many times a for loop should run.

Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

You can remove the @sorted variable and return directly if no swaps were performed, change the loop to while true and add return if swaps == 0 instead of the if swaps ... @sorted =... statements.

You also do not need a class for this, you can either write a top-level function or put it into a module, a class is used to hold data and functions to operate on it, a class that holds a single static function is unreasonable.