ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: zverok.offline@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:84783] [Ruby trunk Feature#14344] refine at class level
Date: Tue, 09 Jan 2018 17:31:08 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-69499.20180109173107.37b98b6cf3d03e0f@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14344.20180109155454@ruby-lang.org

Issue #14344 has been updated by zverok (Victor Shepelev).


> Also I guess in general refinements are not defined right in the class using them, especially if they are not trivial one-liner methods.

I believe that "one-liner" & "inplace" definition is one of the most important usages of refinements.

Because, speaking philosophically, why do we need them at all? 

Instead of doing "`refine String` + call `string.something`" you always can `StringUtilModule.something(string)`, but when you do some, say, complicated reporting algorithm, those `StringUtilModule.something(string)`, especially several in a row can become REALLY ugly. 

Therefore you just... add one small method to `String`. Just here and there, as close to its usage and as visible and as easy as possible, and it WILL be one-line method, just calling the same `StringUtilModule.something(string)` (which is easier to test and maintain and document).

I believe that "module with refinement for the several classes" is, to the opposite, much less frequent case. It is either "all cool shticks for my entire project in one file", or something very domain-specific (like, I don't know, `String#as_currency`, `Numeric#as_money(currency)`, `CSV#ready_money` something).

----------------------------------------
Feature #14344: refine at class level
https://bugs.ruby-lang.org/issues/14344#change-69499

* Author: kddeisz (Kevin Deisz)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I rely on refinements a lot, but don't want to keep writing `Module.new` in code. I'm proposing `Object::refine`, which would create an anonymous module behind the scenes with equivalent functionality. So:

~~~ ruby
class Test
  using Module.new {
    refine String do
      def refined?
        true
      end
    end
  }
end
~~~

would become

~~~ ruby
class Test
  refine String do
    def refined?
      true
    end
  end
end
~~~

It's a small change, but reads a lot more clearly. Thoughts?



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2018-01-09 17:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14344.20180109155454@ruby-lang.org>
2018-01-09 15:54 ` [ruby-core:84778] [Ruby trunk Feature#14344] refine at class level kevin.deisz
2018-01-09 16:38 ` [ruby-core:84779] " shevegen
2018-01-09 17:13 ` [ruby-core:84780] " eregontp
2018-01-09 17:19 ` [ruby-core:84781] " kevin.deisz
2018-01-09 17:26 ` [ruby-core:84782] " eregontp
2018-01-09 17:31 ` zverok.offline [this message]
2018-01-10 16:06 ` [ruby-core:84814] " kevin.deisz
2018-01-10 18:05 ` [ruby-core:84818] " eregontp
2018-01-10 18:22 ` [ruby-core:84819] " kevin.deisz
2018-04-11 14:45 ` [ruby-core:86511] " kevin.deisz
2018-04-16 10:03 ` [ruby-core:86550] " eregontp
2018-04-17 15:38 ` [ruby-core:86561] " kevin.deisz
2019-01-02 22:41 ` [ruby-core:90864] " dementiev.vm
2019-01-11 11:05 ` [ruby-core:91018] " shevegen
2019-01-11 16:04 ` [ruby-core:91026] " dementiev.vm
2019-02-07  6:35 ` [ruby-core:91452] " matz
2019-02-07 21:13 ` [ruby-core:91482] " dementiev.vm
2019-02-07 22:14 ` [ruby-core:91483] " matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-69499.20180109173107.37b98b6cf3d03e0f@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).