Ruby requires different files, but there are two other classes that are almost the same.

Asked 2 years ago, Updated 2 years ago, 35 views

I'm a beginner at ruby.
There are two classes that are almost the same except for the requirements below.
Is there a better way to write this?Thank you for your cooperation.

class Foo 
  require'aaa'#Foo and Bar only have the difference between loading aaa and loading bbb
  def hogehoge
     :
  end
     :
end

class bar 
  require 'bbb'
  def hogehoge
     :
  end
     :
end

ruby

2022-09-30 19:33

1 Answers

First, note the following:

  • Ruby's require is the loading of files into the interprinter and does not deploy in the context where require is written (meaning location, top level, class, or def).Whether it's in class or module, it's always loaded as being at the top level, whether it's in a class or in a module, so it works the same way and it doesn't change the way you put it in the class.
  • When class definitions are evaluated, all statements in the class context are evaluated.The only thing that is not done is what is excluded in the conditional branch and what is inside the def (def defines the method itself is evaluated).
  • Ruby can override the behavior of classes and methods that already exist, so require may change subsequent behavior.

In other words, require behaves the same in and out of the class.With that statement, the behavior does not change immediately before each class definition.

The next important thing is whether bbb changes the behavior of Foo.If it does not change, you can read bb before defining the Foo class.In this code, there is no branch that does not read by conditional branch, so there is no problem if you do require first.In other words, you can write:

require 'aaa'
require 'bbb'

class Foo
  def hogehoge
     :
  end
     :
end

class bar
  def hogehoge
     :
  end
     :
end

Since the class definition is the same, it does not mean that the whole class will be delayed.

Now, the problem is that Foo behavior changes depending on whether bb is loaded or not.In that case, the following problems occur:

    Once
  • require has been loaded, it will never be loaded again.If bbb is loaded elsewhere, it will not work as expected.
  • Originally, libraries should not be built to change their existing behavior.(Standard library has mathn behavior that changes, but is deprecated)

If you really want to change the behavior, be sure to use load to reload.

require 'aaa'

class Foo
  def hogehoge
     :
  end
     :
end

load 'bbb' #must load 'bbb' after Foo
class bar
  def hogehoge
     :
  end
     :
end

However, if bbb is loaded first and the behavior of Foo changes, you should review the implementation of bbb and Foo to avoid relying on read timing.

Because of the functional enable/disable conditional branch, it is often constructed to require libraries that are only required when enabled, but otherwise, all require are placed at the beginning of the file.If you don't want to know whether Foo or Bar, you should have Foo and Bar files separately.If you don't have a tool for doing simple things in one file, then one class and one file will be the basis.

Now, require has been managed.Suppose you could write everything at the beginning of the file.Next, to summarize the commonality, there are many ways to do this, but we usually use class inheritance. If the hogehoge were exactly the same, we should be able to write as follows:

require 'aaa'
require 'bbb'

class FooBar
  def hogehoge
     :
  end
     :
end

class Foo<FooBar
     :
end

class Bar <FooBar
     :
end

Foo and Bar inherit FooBar.Both Foo and Bar can use the hogehoge methods unless defined separately (called overrides), which are the hogehoge methods of FooBar.This FooBar is referred to as the parent class of Foo and Bar, and Foo is referred to as the child class of FooBar.By using class inheritance in this way, you can consolidate common parts into the parent class, and only implement differences in the child class.

In addition to inheriting classes, Mixin is another way to summarize commonality.This is one of Ruby's basic features that must be included in the introduction along with the succession of classes, so please refer to books for more information.


2022-09-30 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.