New answers tagged

3

Expanding on @jpemberthy: Shouldn't we duck type? def pick_for_game(game) game = game.id if game.respond_to? :id picks.find_or_initialize_by_game_id(game) end or be brazen: def pick_for_game(game) picks.find_or_initialize_by_game_id(begin game.id rescue game end) end


0

Ruby Hash Rather than using a verbose case statement: case index when 0 0.2 when 1 0.25 when 2 0.3 You could use a Hash: rates = {0 => 0.2, 1 => 0.25, 2 => 0.3} And the case can be replaced with: rates[index] Rubocop Report Use symbols as procs when possible. covers = covers.map { |x| x.to_sym } ...


1

Rubocop Report connectedCell Avoid nested code blocks if clean alternative statements are available. For instance, in method connectedCell you have the following block: if matrix[i][j] == 1 # .. code omitted end Replace the if-statement with next unless matrix[i][j] == 1 and you'll be able to reduce nesting with 1 level. The next part has a similar ...


1

I really like clear and speaking method names, I quickly tried to refactor the first method but have had not much time, I hope this helps anyway. I have not understand the other initialization of the visited hash in the area_count method (yet). The new neighbors method indeed looks some kind of overloaded with information, no good idea yet how to change it,...


2

In addition to what @dfhwze has said, I wanted to give some suggestions on general ruby best practice and readibility: Multiline Blocks In PriceCalculator#start_billing, PriceCalculator#count_items and in a couple other methods you use muliline blocks like so: foobar.each { |*args| # do stuff # more stuff } The use of {} is really only intended for ...


0

Rubocop Report Conventions Prefer key? over has_key? (PreferredHashMethods). if params.has_key?(:thumbnail) if params.key?(:thumbnail) Prefer single quoted over double quoted string literals if you don't need interpolation or special symbols (StringLiterals). image.resize("50x60") image.resize('50x60') Complexity If block in a separate, private ...


2

Actually, Active Record can work with joined collection (including has_many :through) from the box. So, you just need to pass new collection instead of old, and AR will delete excessive and add new records. In your code, need to change create method body to: gig = Gig.find(params[:gig_id]) singer_ids = params[:singer_ids] # [1, 4, 5,] gig.singers = Singer....


3

nth slicing Perhaps you could use a slice with map instead. module Enumerable def nth(n) self.each_slice(n).select{|c| c.length == n}.map(&:last) end end unit tests print [2, 4, 6, 8, 10, 12].nth(3) # => [6, 12] print "\n" print [2, 4, 6, 8, 10].nth(3) # => [6] print "\n" print [2, 4, 6, 8].nth(3) # => [6] print "\n" print [2, 4, ...


Top 50 recent answers are included