<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>doraemon0711 (ddd)</title>
    <link>https://ruby-china.org/doraemon0711</link>
    <description/>
    <language>en-us</language>
    <item>
      <title>请教一下，ruby 中能否复制某个 class 的方法到当前 class 中</title>
      <description>&lt;p&gt;举个例子
以下是个父类&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:flag&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_flag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; - &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这时有个子类&lt;code&gt;BizA&lt;/code&gt;继承了&lt;code&gt;Base&lt;/code&gt;，并实现了&lt;code&gt;exec&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BizA&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;现在如果执行&lt;code&gt;BizA.new.exec.print_flag&lt;/code&gt;，会输出&lt;code&gt;BizA - true&lt;/code&gt;
上面是前提&lt;/p&gt;

&lt;p&gt;我的问题是，如果有领个子类&lt;code&gt;BIzB&lt;/code&gt;，能否在&lt;code&gt;BizB&lt;/code&gt;继承&lt;code&gt;Base&lt;/code&gt;的情况下，拥有和 BizA 相同实现的&lt;code&gt;exec&lt;/code&gt;方法&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BizB&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="c1"&gt;# 不去实现exec，而是复制BizA的exec到当前class&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;然后在执行&lt;code&gt;BizB.new.exec.print_flag&lt;/code&gt;时，输出&lt;code&gt;BizB - true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;我知道的几种方式在这里列一下，但不是我想要的效果&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;BizB 继承 BizA - 这种方式确实能正确输出想要的结果，但在我看来 BizB 和 BizA 实际上是同级的，只不过 BizB &lt;em&gt;委托&lt;/em&gt; BizA 中的方法去进行处理，是在没有别的办法的话我会采取该方案&lt;/li&gt;
&lt;li&gt;使用 DelegateClass 委托 - 这样做不符合需求，因为在执行&lt;code&gt;BizB.new.exec.print_flag&lt;/code&gt;时，必须要提供一个 BizA 的实例，而 exec 内部获取 self 时获取到的也是 BizA 的实例，会出现问题&lt;/li&gt;
&lt;li&gt;将 exec 抽到 module 中使用 include 的方式 - 这样做有些偏离初衷了，并且改动加到，我当前的场景是在实现&lt;code&gt;Base&lt;/code&gt;的基类，给用户继承使用，&lt;code&gt;Base&lt;/code&gt;中还有很多其他的方法和属性，所以不考虑&lt;/li&gt;
&lt;/ol&gt;</description>
      <author>doraemon0711</author>
      <pubDate>Wed, 22 Jan 2025 15:55:39 +0800</pubDate>
      <link>https://ruby-china.org/topics/44028</link>
      <guid>https://ruby-china.org/topics/44028</guid>
    </item>
    <item>
      <title>请教一下 gem 依赖的 gem 的 constant 冲突了怎么解决 </title>
      <description>&lt;p&gt;用到了 a 和 b 两个 gem&lt;br&gt;
a gem 依赖了 &lt;a href="https://gems.ruby-china.com/gems/protobuf" rel="nofollow" target="_blank" title=""&gt;protobuf&lt;/a&gt;&lt;br&gt;
b gem 依赖了 &lt;a href="https://gems.ruby-china.com/gems/google-protobuf" rel="nofollow" target="_blank" title=""&gt;google-protobuf&lt;/a&gt;&lt;br&gt;
现在启动程序会提示很多警告&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:11: warning: already initialized constant Google::Protobuf::FileDescriptorSet
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:15: warning: previous definition of FileDescriptorSet was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:12: warning: already initialized constant Google::Protobuf::FileDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:16: warning: previous definition of FileDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:13: warning: already initialized constant Google::Protobuf::DescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:17: warning: previous definition of DescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:16: warning: already initialized constant Google::Protobuf::ExtensionRangeOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:23: warning: previous definition of ExtensionRangeOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:17: warning: already initialized constant Google::Protobuf::FieldDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:24: warning: previous definition of FieldDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:20: warning: already initialized constant Google::Protobuf::OneofDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:54: warning: previous definition of OneofDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:21: warning: already initialized constant Google::Protobuf::EnumDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:55: warning: previous definition of EnumDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:23: warning: already initialized constant Google::Protobuf::EnumValueDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:60: warning: previous definition of EnumValueDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:24: warning: already initialized constant Google::Protobuf::ServiceDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:61: warning: previous definition of ServiceDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:25: warning: already initialized constant Google::Protobuf::MethodDescriptorProto
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:62: warning: previous definition of MethodDescriptorProto was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:26: warning: already initialized constant Google::Protobuf::FileOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:63: warning: previous definition of FileOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:28: warning: already initialized constant Google::Protobuf::MessageOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:72: warning: previous definition of MessageOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:29: warning: already initialized constant Google::Protobuf::FieldOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:73: warning: previous definition of FieldOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:32: warning: already initialized constant Google::Protobuf::OneofOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:88: warning: previous definition of OneofOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:33: warning: already initialized constant Google::Protobuf::EnumOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:89: warning: previous definition of EnumOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:34: warning: already initialized constant Google::Protobuf::EnumValueOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:90: warning: previous definition of EnumValueOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:35: warning: already initialized constant Google::Protobuf::ServiceOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:91: warning: previous definition of ServiceOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:36: warning: already initialized constant Google::Protobuf::MethodOptions
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:92: warning: previous definition of MethodOptions was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:38: warning: already initialized constant Google::Protobuf::UninterpretedOption
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:101: warning: previous definition of UninterpretedOption was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:40: warning: already initialized constant Google::Protobuf::SourceCodeInfo
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:106: warning: previous definition of SourceCodeInfo was here
/gems/google-protobuf-3.19.3-x86_64-darwin/lib/google/protobuf/descriptor_pb.rb:42: warning: already initialized constant Google::Protobuf::GeneratedCodeInfo
/gems/protobuf-3.10.5/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb:111: warning: previous definition of GeneratedCodeInfo was here
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;不知道是不是因为这个原因 spring 经常卡住 - -&lt;/p&gt;</description>
      <author>doraemon0711</author>
      <pubDate>Thu, 31 Mar 2022 22:36:50 +0800</pubDate>
      <link>https://ruby-china.org/topics/42273</link>
      <guid>https://ruby-china.org/topics/42273</guid>
    </item>
  </channel>
</rss>
