From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id B74311960013 for ; Tue, 30 Jun 2015 19:01:28 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id B3F2EB5D948 for ; Tue, 30 Jun 2015 19:26:47 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 026E597A82C for ; Tue, 30 Jun 2015 19:26:50 +0900 (JST) X-Virus-Scanned: amavisd-new at nagaokaut.ac.jp Authentication-Results: funfun.nagaokaut.ac.jp (amavisd-new); dkim=fail (1024-bit key) reason="fail (message has been altered)" header.d=sendgrid.me Received: from funfun.nagaokaut.ac.jp ([127.0.0.1]) by funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NFvmcIpjxWQs for ; Tue, 30 Jun 2015 19:26:49 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 62CDE97A82B for ; Tue, 30 Jun 2015 19:26:49 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id DF22195243A for ; Tue, 30 Jun 2015 19:26:45 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A1D9B1204BD; Tue, 30 Jun 2015 19:26:44 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 68590120495 for ; Tue, 30 Jun 2015 19:26:41 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=D1Tgkv2LcmZGpDpx/2f0xhJefLE=; b=Tl9u607qxP+K0lZIkd UD4JXK0HK89GtpsOEP7M7h0grzNaRmcxCGNXTKFRzSLp+QxVeG1KwI2mWQ4YW058 patFGaU81umvTtaYJzadLzax5vI7LFIpsbiDMjkYjpOYetiBVNfZzoUwkfGEQzPJ 7G2nM/VCRreJYK+oHFo822ewI= Received: by filter0593p1mdw1.sendgrid.net with SMTP id filter0593p1mdw1.7120.55926ED82D 2015-06-30 10:26:32.951169247 +0000 UTC Received: from herokuapp.com (ec2-54-163-4-232.compute-1.amazonaws.com [54.163.4.232]) by ismtpd-044 (SG) with ESMTP id 14e4400fea9.7459.769cb9 Tue, 30 Jun 2015 10:26:32 +0000 (UTC) Date: Tue, 30 Jun 2015 10:26:32 +0000 From: 0x0dea+redmine@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 44411 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 10594 X-Redmine-Issue-Author: findchris X-Redmine-Sender: 0x0dea X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS53BT1RPjzjT6rk8IiGosBkYjLehKA2tTj9N3 OtsDRjTUCzVzZ3hdyEtHx4SeK9BB2J6rqe96ov+svx0O1gyEKB+J5np5l6VuE3R4+EyaEsGg+tFy3/ PX6Qaz9VdZSinVU2H86N9gkCzZQkSa+pTXY4 X-ML-Name: ruby-core X-Mail-Count: 69815 Subject: [ruby-core:69815] [Ruby trunk - Bug #10594] Numeric#clamp X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #10594 has been updated by D.E. Akers. Hans Mackowiak wrote: > hm might it be a good idea to have such a function directly in Comparable too? > > like "x".chomp("a".."e") #=> "e" > hm maybe have it a second way to call it with using "x".chomp("a", "e") too similar to Comparable#between? I think your suggestions make a great deal of sense. That `#clamp` should be defined in terms of `#<=>` makes `Comparable` its natural home. Additionally, if the two-argument form were the only way to call it, the implementation would pretty much be the same as for `#between?` but with greater information density: ```diff static VALUE -cmp_between(VALUE x, VALUE min, VALUE max) +cmp_clamp(VALUE x, VALUE min, VALUE max) { - if (RTEST(cmp_lt(x, min))) return Qfalse; + if (RTEST(cmp_lt(x, min))) return min; - if (RTEST(cmp_gt(x, max))) return Qfalse; + if (RTEST(cmp_gt(x, max))) return max; - return Qtrue; + return x; } ``` This approach does away with the potential for confusion introduced by exclusive ranges, made worse by the fact that "predecessor" is not well-defined for most `Comparable`s. ---------------------------------------- Bug #10594: Numeric#clamp https://bugs.ruby-lang.org/issues/10594#change-53213 * Author: Chris Johnson * Status: Open * Priority: Normal * Assignee: * ruby -v: 2.1.2 * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN ---------------------------------------- This is basically a re-opening of the feature request of issue#4573 (https://bugs.ruby-lang.org/issues/4574), which was closed due a naming debate. It seems the standard naming for restricting a number to a specified range is indeed 'clamp'. (1)(2)(3) As such, can we use Yusuke Endoh's original patch with the naming adjustments? If so, I can provide accordingly. Cheers. (1) http://www.rubydoc.info/github/epitron/epitools/Numeric:clamp (2) http://stackoverflow.com/questions/12020787/is-there-a-limit-clamp-function-in-ruby (3) https://developer.gnome.org/glib/stable/glib-Standard-Macros.html#CLAMP:CAPS ---Files-------------------------------- num_clamp.c (427 Bytes) -- https://bugs.ruby-lang.org/