2014-04-17 2 views
2

Ruby 연습에 PullReview을 사용하기 시작했습니다. 다음과 같은 오류가 발생합니다.전역 변수를 사용하지 않고 Ruby에 넣는 대신 Logger를 사용하는 방법

Use a logger instead of puts or add assertion. 

require_relative '../lib/soshisoai' 

file_path = File.expand_path('../../spec/fixtures/soshisoai3.txt', __FILE__) 

myarr1 = Soshisoai.parse_file(file_path) 
myarr2 = Soshisoai.get_combi(myarr1) 
myarr3 = Soshisoai.flat_arr(myarr2) 
myarr4 = Soshisoai.eliminate_duplicate(myarr3) 
myarr5 = Soshisoai.swap_male(myarr4) 
myarr6 = Soshisoai.find_match(myarr5) 
myarr7 = Soshisoai.delete_same_suffix(myarr6) 
myarr8 = Soshisoai.delete_same_prefix(myarr7) 
puts myarr8 

Why 

You don't want to clutter your logs with raw puts, pp, or p. Output using p 
will not always appear in your logs, nor will it inherit from any log config 
you may have (to add information such as the timestamp). 

How to fix 

In a Rails application 
Use Rails.logger instead. 

In Ruby code 
Just use the Logger class. 

In unit and integration tests 
This is often a sign that you are missing some asserts and other checks. 

그런 다음 Logger를 사용했지만 다른 오류가 발생했습니다.

Avoid global variable. 

require_relative '../lib/soshisoai' 
require 'logger' 

$Log = Logger.new('log_file.log') 
$Log.debug('Soshisoai3') 
file_path = File.expand_path('../../spec/fixtures/soshisoai3.txt', __FILE__) 

myarr1 = Soshisoai.parse_file(file_path) 
myarr2 = Soshisoai.get_combi(myarr1) 
myarr3 = Soshisoai.flat_arr(myarr2) 
myarr4 = Soshisoai.eliminate_duplicate(myarr3) 
myarr5 = Soshisoai.swap_male(myarr4) 
myarr6 = Soshisoai.find_match(myarr5) 
myarr7 = Soshisoai.delete_same_suffix(myarr6) 
myarr8 = Soshisoai.delete_same_prefix(myarr7) 
$Log.debug(myarr8) 

Why 

This check reports global variables. Global variables introduce strong dependencies 
between otherwise unrelated parts of code and their usage is usually considered 
extremely bad style. 

How to fix 

If you need a variable in many different places, here are some options other 
than using a global 
... 

어떻게 이러한 오류가 발생하지 않도록 할 수 있습니까?

답변

0

방법

@log = Logger.new을 변수 예 ('log_file.log')를 사용하는 방법에 대한

그런 다음 당신은 동일한 개체 내에서 @log 사용할 수 있습니다.

2

그냥 않는 Rails.logger으로 수행 : 사용자가 원하는 클래스 중 하나의 인스턴스 변수를 만드는 (당신이 레일로, 그렇지 않으면 당신은 단지뿐만 아니라 Rails.logger 자체를 사용할 수 있습니다 사용 하지입니다 가정). 객체가 아닌 클래스의 인스턴스 변수를 사용해야합니다. 기술적으로 이것은 이며, 전체 변수는이지만, PullReview이라는 불만을 피하려면 충분해야합니다.

require 'logger' 

class MyApp 
    @logger=Logger.new("/tmp/log") 
    def self.logger 
    @logger 
    end 
end 

같은 뭔가 당신이 불평없이 MyApp.logger를 호출 물론 당신은 당신이 원하는 무엇이든 로깅 클래스 무료로 사용할 수있을 것입니다 허용해야합니다.

관련 문제