ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "matheusrich (Matheus Richard) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "matheusrich (Matheus Richard)" <noreply@ruby-lang.org>
Subject: [ruby-core:116369] [Ruby master Feature#20202] Memoized endless method definitions
Date: Mon, 22 Jan 2024 16:40:37 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-20202.20240122164037.49162@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20202.20240122164037.49162@ruby-lang.org

Issue #20202 has been reported by matheusrich (Matheus Richard).

----------------------------------------
Feature #20202: Memoized endless method definitions
https://bugs.ruby-lang.org/issues/20202

* Author: matheusrich (Matheus Richard)
* Status: Open
* Priority: Normal
----------------------------------------
I propose introducing a shorthand for memoized endless method definitions:

```rb
class Foo
  def bar ||= :memoized_value

  # It should behave like
  def bar = (@bar ||= :memoized_value)
end
```

Not only is this shorter and (IMO) a natural follow-up for endless method definitions, but it's also a common pattern on Ruby codebases. It's very useful to decompose responsibilities into several objects:

```rb
class User
  def notifications_enabled? = settings.notifications?
  def enable_notifications = (settings.notifications = true)
  def disable_notifications = (settings.notifications = false)

  private

  def settings = @settings ||= Settings.new(self)
end

class User::Settings
  attr_writer :notifications

  def initialize(user)
    @user = user
    @notifications = false
  end

  def notifications? = @notifications
end

u = User.new
u.notifications_enabled? # => false
u.enable_notifications
u.notifications_enabled? # => true
```

In the example, the `settings` method could be rewritten as

```rb
  def settings ||= Settings.new(self)
```



-- 
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/

       reply	other threads:[~2024-01-22 16:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22 16:40 matheusrich (Matheus Richard) via ruby-core [this message]
2024-01-22 19:56 ` [ruby-core:116372] [Ruby master Feature#20202] Memoized endless method definitions rubyFeedback (robert heiler) via ruby-core
2024-01-22 20:29 ` [ruby-core:116373] " matheusrich (Matheus Richard) via ruby-core
2024-02-06 19:32 ` [ruby-core:116608] " matheusrich (Matheus Richard) via ruby-core
2024-03-14  9:04 ` [ruby-core:117146] " matz (Yukihiro Matsumoto) via ruby-core

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.issue-20202.20240122164037.49162@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-core@ml.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).