[OT] Opinions about using enumerable for "lexical grouping" of related method definitions (idiomatic?)

I'm trying to devise a way to use lexical structures in Ruby to group
method definitions while reducing repetition at the same time. Yes, of
course, the usefulness of this in my example is debatable. Question is, can
it be considered idiomatic Ruby to use the enumerable methods in this
fashion? I like that I *can* do it but I'm not sure if I *should*.

···

# Messages
  [Message].map do |table_class|
    define_method(:inbox) { table_class.where(recipient: self) }
    define_method(:outbox) { table_class.where(sender: self) }
  end

The first thoughts to pop up in my mind:

How would you test those methods?

Compare to having them defined in separate classes or modules.

Cheers

Christer

···

tis 7 juli 2020 kl. 09:07 skrev Liam <liamelliott1123@gmail.com>:

I'm trying to devise a way to use lexical structures in Ruby to group
method definitions while reducing repetition at the same time. Yes, of
course, the usefulness of this in my example is debatable. Question is, can
it be considered idiomatic Ruby to use the enumerable methods in this
fashion? I like that I *can* do it but I'm not sure if I *should*.

# Messages
  [Message].map do |table_class|
    define_method(:inbox) { table_class.where(recipient: self) }
    define_method(:outbox) { table_class.where(sender: self) }
  end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
Med vänlig hälsning,

Christer Jansson

Christer Jansson Datakonsult AB
+46 70 88 55 020

It’s hard for me to give a clear opinion on this with such little context, but it seems you could achieve this via a standard module usage - like so:

module Messegeable
  def inbox
    where(recipient: self)
  end

  def outbox
    where(sender: self)
  end
end

class Message
  extend Messegeable
  
  # …
end

···

On 7 Jul 2020, at 08:06, Liam <liamelliott1123@gmail.com> wrote:

I'm trying to devise a way to use lexical structures in Ruby to group method definitions while reducing repetition at the same time. Yes, of course, the usefulness of this in my example is debatable. Question is, can it be considered idiomatic Ruby to use the enumerable methods in this fashion? I like that I can do it but I'm not sure if I should.

# Messages
  [Message].map do |table_class|
    define_method(:inbox) { table_class.where(recipient: self) }
    define_method(:outbox) { table_class.where(sender: self) }
  end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Hey Tom <3

I *think* you've made a mistake there.

It seems that `Message` is a model.
And the code is meant to introduce helper methods that interact with that model

module Messageable
  def inbox
    Message.where(recipient: self)
  end

  def outbox
    Message.where(sender: self)
  end
end

# other active record models
User.include(Messageable)
Bot.include(Messageable)

However, why not just use associations?

has_many :messages

Liam, I am not sure what's `[Message].map` is about. It feels like you want some kind of flexibility
around models used but I can't see what exactly. Could you provide more context on what you are doing
so people are able to provide more specific opinion?

···

On 7 Jul 2020, at 09:27, Tom Lord <lord.thom@gmail.com> wrote:

It’s hard for me to give a clear opinion on this with such little context, but it seems you could achieve this via a standard module usage - like so:

module Messegeable
  def inbox
    where(recipient: self)
  end

  def outbox
    where(sender: self)
  end
end

class Message
  extend Messegeable
  
  # …
end

On 7 Jul 2020, at 08:06, Liam <liamelliott1123@gmail.com> wrote:

I'm trying to devise a way to use lexical structures in Ruby to group method definitions while reducing repetition at the same time. Yes, of course, the usefulness of this in my example is debatable. Question is, can it be considered idiomatic Ruby to use the enumerable methods in this fashion? I like that I can do it but I'm not sure if I should.

# Messages
  [Message].map do |table_class|
    define_method(:inbox) { table_class.where(recipient: self) }
    define_method(:outbox) { table_class.where(sender: self) }
  end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>