Y
Y
Yunique2018-08-07 12:00:32
ruby
Yunique, 2018-08-07 12:00:32

How to return the value of a variable from a function?

I am new to ruby. Faced such a problem

def get_old_stats
    old_stat = Set.new
    last_log = @campaign.job_logs.where("slm_dump_path is NOT NULL").first
    return unless last_log
    dump_path = last_log.slm_dump_path
    raise "slm_dump_path is missing for #{@campaign}" unless dump_path
    S3::Archive.new.download(dump_path).each do |id|
      old_stat.add(id.first)
    
  end

It is necessary that this function return the value of old_stat
If I write return old_stat or simply old_stat at the end - it returns nil
Works if you only make the old_stat variable an instance variable (@old_stat) and take it out into a separate function and call it from this separate function
def get_old_stat
    @old_stat
  end

But I want to get rid of such a clutter and limit myself to one function, how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AVKor, 2018-08-07
@AVKor

#!/usr/bin/env ruby

def test
  testvar = 'foo'
end
puts test

$ ./test.rb 
foo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question