A little bad example, but a personal note.
↓ I wanted to write something like this in Ruby.
const filepath = (() => {
return "hoge/moge/huga";
})();
You can define it normally, but if the processing part gets messed up, I wanted to separate the variable definition from the processing part, so I thought about it.
class Hoge
def initialize
@root = root
end
def filepath(path)
File.join(@root.call, path)
end
private
def root
#Assignment to path is initialization@Only when assigned to root
path = "hoge/moge/huga"
-> { path }
end
end
hoge = Hoge.new
p hoge.filepath("a") # => "hoge/moge/huga/a"
p hoge.filepath("i") # => "hoge/moge/huga/i"
p hoge.filepath("u") # => "hoge/moge/huga/u"
p hoge.filepath("e") # => "hoge/moge/huga/e"
p hoge.filepath("o") # => "hoge/moge/huga/o"
The path
defined in the root
method of this example is written directly, but if you want to store some processing result in the path
and reuse it, you do not need to run the processing many times. ,Good vibes.
Recommended Posts