[Question] For the sequel blank plugin, why we need define a blank? alias use alias_method?

5 views
Skip to first unread message

Billy Zheng

unread,
Aug 4, 2021, 10:35:39 AMAug 4
to sequel-talk
I write a gem use the code from `sequel/extensions/blank`, when i reading following code, i don't understand why we need run `alias_method` like following?

```rb
[FalseClass, Object, NilClass, Numeric, String, TrueClass].each do |klass|
  # :nocov:
  if klass.method_defined?(:blank?)
    klass.send(:alias_method, :blank?, :blank?)
  end
  # :nocov:
end
```

Thank you.

Jeremy Evans

unread,
Aug 4, 2021, 10:43:17 AMAug 4
to seque...@googlegroups.com
This self-aliasing is done to avoid warning messages in verbose warning mode if an existing blank? method is already defined.  It's the officially recommended way of avoiding the warning messages.

Thanks,
Jeremy

Billy Zheng

unread,
Aug 4, 2021, 10:48:59 AMAug 4
to sequel-talk
Oh, thank you.

One more question, do you consider add support for `present?`,  i wrote code like this at the end of that file.

```
[FalseClass, Object, NilClass, Numeric, String, TrueClass].each do |klass|
  # :nocov:
  klass.define_method(:present?) { !blank? }
  # :nocov:
end
```

Jeremy Evans

unread,
Aug 4, 2021, 11:12:54 AMAug 4
to seque...@googlegroups.com
On Wed, Aug 4, 2021 at 7:49 AM Billy Zheng <vil...@gmail.com> wrote:
Oh, thank you.

One more question, do you consider add support for `present?`,  i wrote code like this at the end of that file.

No.  The only reason the blank extension exists is for backwards compatibility. It was originally used for other internal features, but Sequel::Database#blank_object? is now used for that (this will call blank? on the object if the object responds to it, also for backwards compatibility).  I don't want to add any new features to the blank extension.  I also do not want to add any extensions that are general modifications to core classes and not specifically related to Sequel. The support for blank? was added before I took over maintenance.

Thanks,
Jeremy

Billy Zheng

unread,
Aug 4, 2021, 11:50:22 PMAug 4
to sequel-talk

Yes, i totally agree with you. we should not add a core class extension in sequel, maybe the correct way is extract those code into a individual gem, then add present? method, just like what i done. haha
Reply all
Reply to author
Forward
0 new messages