New answers tagged

1

What is the use case and rationale for this module? I can see what it does, but I'm struggling to understand why I'd want to do it. The fact that you have three almost identical case statements to drive the behaviour is a 'code smell'. In most other OO languages you'd use inheritance or an interface and a factory method to determine which subclass of an ...


0

Ruby's Enumerable has a large number of useful methods, which often have additional behaviours when you pass in parameters or a block. It's well worth the time invested in reading about them, as pretty much anything you want to do with an iterator has already been implemented. I learned something new about count today from @superb rain, because I'd ...


6

Your method shouldn't call the given array my_array. It's not the method's array, it's the caller's array. Just call it array, which is also shorter, so less clutter. And you're making it very complicated. The array already offers all you need: def max_number_count(array) array.count(array.max) end That's also much faster: user system ...


0

The objects in the priority queue (as implemented above) have their priority determined by <, >, and ==. So however equality is defined for the object determines its priority. For numbers and strings, this is fairly obvious but the default implementation of equality, less than and greater than for hashes may surprise you and not give the result you are ...


2

Consistency Sometimes you are using 1 space for indentation, sometimes you are using 2, sometimes 4, sometimes none. Sometimes you are using whitespace around operators, sometimes you don't, sometimes you are using whitespace on one side of the operator, but not the other. Sometimes, you use space after a comma, sometimes you don't. Sometimes, you have a ...


1

Consistency Sometimes you are using 1 space for indentation, sometimes you are using 2. Sometimes you are using parentheses around the arguments of a message send, sometimes you don't. Sometimes you are using parentheses around the condition of a conditional, sometimes you don't. Sometimes you are using new-style hash syntax, sometimes you are using old-...


1

Consistency Sometimes you are using 1 space for indentation, sometimes you are using 2. Sometimes you are using whitespace around operators, sometimes you don't, sometimes you are using whitespace on one side of the operator, but not the other. Sometimes, you use space after a comma, sometimes you don't. Sometimes you use one empty line after a method, ...


2

As you say, the game is fully functional. But from a design perspective, it could use a little work. My number one issue is that the whole game is all in one class, and the whole game is run through the initialize method, which only finishes at the end of the game The turn method calls victory_check, which then recursively calls turn until the end of the ...


2

First of all I also agree with the questions asked that you should define first why you want to optimize this method? With 4 lines of code I think this method is still readable and also your suggested refactoring would more obfuscate this method. Anyway, here are some suggestions to improve the readability a little bit. This is just a small refactoring but I ...


1

Noticed that you call new_method.pluralize five times in each pass through the loop. It's a small thing, but setting a variable once on each pass through would be slightly more efficient...


-1

It's a method on Enumerable. It is also aliased to find which may be a more descriptive verb depending on the context. Using the built-in functions will always be faster because they are implemented in c


1

One-liners are good fun, but the world doesn't really need more of them. That said, they don't have to be so unreadable. What will your future brain say a year from now if you have to maintain that function? Here's an approach illustrating a scalable technique to make even long "one-liners" readable by (1) using lines generously, (2) indenting code ...


Top 50 recent answers are included