From: "e8c (Viktor Reznov) via ruby-core" Date: 2023-01-01T10:21:38+00:00 Subject: [ruby-core:111560] [Ruby master Feature#3591] Adding Numeric#divisor? (Have working implementation) Issue #3591 has been updated by e8c (Viktor Reznov). matz (Yukihiro Matsumoto) wrote in #note-19: > Is it more useful than `n % m == 0`? Yes, statement `n .has_divisor? m` (or `m .divisor_of? n`) has 3 parts instead of 5, and gives a direct answer to the question. The "problem": https://leetcode.com/problems/count-the-digits-that-divide-a-number/ Two solutions: ```ruby def count_digits(n) = n.digits.count { n % _1 == 0 } def count_digits(n) = n.digits.count { _1.divisor_of?(n) } ``` The first is shorter, but the second is clearer. ---------------------------------------- Feature #3591: Adding Numeric#divisor? (Have working implementation) https://bugs.ruby-lang.org/issues/3591#change-100925 * Author: duckinator (Marie Markwell) * Status: Rejected * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: 2.6 ---------------------------------------- =begin On the 'duckinator' branch of my fork of ruby on github, I have added Numeric#divisor?: num.divisor?(other) is the same as: (num % other == 0) Example usage: 4.divisor?(2) #=> true 1.divisor?(2) #=> false 2.4.divisor?(1.2) #=> true 2.4.divisor?(1.3) #=> false 126.divisor?(9) #=> true I think this would be a very nice feature to add, and would make code using the old (num % other == 0) method much cleaner and easier to understand. There is a unified diff of it: http://gist.github.com/raw/484144/07b1a6e696cd9301e658ccbc8f90dfcd4d4ef3f1/Numeric_divisor.patch The original commits can be seen here: http://github.com/RockerMONO/ruby/commit/f7959f964cb0bf38418904ccb5643db6b689d29c -- First attempt, only worked with Integers http://github.com/RockerMONO/ruby/commit/12376a6bb1c3ffbd4b470850dd758e2dcd783dda -- Second attempt, should work with anything derived from Numeric that can be used as (num % other == 0) =end -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/