Skip to content

Commit 2cb0242

Browse files
committed
DRY code
1 parent 1ac3d4e commit 2cb0242

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/lightgbm/booster.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ def initialize(params: nil, train_set: nil, model_file: nil, model_str: nil)
77
model_from_string(model_str)
88
elsif model_file
99
out_num_iterations = ::FFI::MemoryPointer.new(:int)
10-
::FFI::MemoryPointer.new(:pointer) do |handle|
10+
create_handle do |handle|
1111
check_result FFI.LGBM_BoosterCreateFromModelfile(model_file, out_num_iterations, handle)
12-
@handle = ::FFI::AutoPointer.new(handle.read_pointer, FFI.method(:LGBM_BoosterFree))
1312
end
1413
else
1514
params ||= {}
1615
set_verbosity(params)
17-
::FFI::MemoryPointer.new(:pointer) do |handle|
16+
create_handle do |handle|
1817
check_result FFI.LGBM_BoosterCreate(train_set.handle_pointer, params_str(params), handle)
19-
@handle = ::FFI::AutoPointer.new(handle.read_pointer, FFI.method(:LGBM_BoosterFree))
2018
end
2119
end
2220

@@ -102,9 +100,8 @@ def feature_name
102100

103101
def model_from_string(model_str)
104102
out_num_iterations = ::FFI::MemoryPointer.new(:int)
105-
::FFI::MemoryPointer.new(:pointer) do |handle|
103+
create_handle do |handle|
106104
check_result FFI.LGBM_BoosterLoadModelFromString(model_str, out_num_iterations, handle)
107-
@handle = ::FFI::AutoPointer.new(handle.read_pointer, FFI.method(:LGBM_BoosterFree))
108105
end
109106
@cached_feature_name = nil
110107
self
@@ -200,6 +197,13 @@ def handle_pointer
200197
@handle
201198
end
202199

200+
def create_handle
201+
::FFI::MemoryPointer.new(:pointer) do |handle|
202+
yield handle
203+
@handle = ::FFI::AutoPointer.new(handle.read_pointer, FFI.method(:LGBM_BoosterFree))
204+
end
205+
end
206+
203207
def eval_counts
204208
out = ::FFI::MemoryPointer.new(:int)
205209
check_result FFI.LGBM_BoosterGetEvalCounts(handle_pointer, out)

0 commit comments

Comments
 (0)