[Solved] (Was: Re: Need to advice: i want to reduce code lines)
[email protected] (Byung-Hee HWANG (황병희, 黃 炳熙))
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <[email protected]> |
Brandon Weaver <[email protected]> writes: > The main thing you can do is not use regex to get each consecutive two digits of a string. Just use indexes on strings. > > An extract_date function might look something like this once cleaned up: > > def extract_date(date_string) > year = "20#{date_string[0..1]}".to_i > month = date_string[2..3].to_i > day = date_string[4..5].to_i > > DateTime.new(day, month, year, 0, 0, 0, 0.375) > end Above code are very useful, thanks!!! > I would leave the offsetting (21 and 35) outside of this function, as it's a separate concern. I would also give names to those values, which I'd guess are 3 and 5 week intervals respectively. You are right!!! 21 -> 3 week, 35 -> 5 week. > Now on to the unix code you have above, there are ways to get the same data in Ruby: > > def load_data(source_url: FULL_URL) > Net::HTTP > .get(URI(source_url)) > # Gets the lines of a String > .lines > # Lines that start with 4 digits and have NA in there, avoids blanks > .grep(/^\d{4}.*NA/) > # Use the | delimiter to get the 1st and 3rd value > .map { |line| line.split(/ | /).values_at(0, 2) } > # Since you want in groups of the 1st and 3rd this would line them > # back up > .transpose > end Are yes, but i need to study more for understanding your codes. Anyway thanks a lot! > There are some which would (*fairly) say that transposing and extracting the data isn't the concern of that function. That'd be a refactoring left as an exercise to the reader, as I'm not aiming for perfect code > in this, just demonstrating ideas. Thanks again for detail comments ;;; Sincerely, Byung-Hee from South Korea -- ^고맙습니다 _地平天成_ 감사합니다_^))// Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
001.diff
(text/x-diff, 1014 B)
--- z001.rb.soyeomul_version 2019-01-22 22:18:50.048000179 +0900
+++ z001.rb 2019-01-22 22:28:21.740000213 +0900
@@ -19,34 +19,16 @@
def _make_oday(n)
- exp = Regexp.new("^([0-9]{2})([0-9]{2})([0-9]{2})$")
-
- rs = n.to_s
-
- ix = exp.match(rs)[1]
- iy = exp.match(rs)[2]
- iz = exp.match(rs)[3]
-
- sx = "20"+ix.to_s
- x = sx.to_i
-
- if iy[0].chr == '0'
- y = iy[1].chr.to_i
- else
- y = iy.to_i
- end
-
- if iz[0].chr == '0'
- z = iz[1].chr.to_i
- else
- z = iz.to_i
- end
-
+ x = "20#{n[0..1]}".to_i
+ y = n[2..3].to_i
+ z = n[4..5].to_i
+
oday = DateTime.new(x, y, z, 0, 0, 0, 0.375)
return oday
end
# 분만 예정일 6자리 숫자를 분석하여 날짜양식으로 형변환
+# Thanks to: Brandon Weaver on gmane.comp.lang.ruby.general
def the_day_1(n)
_make_oday(n) - 35
@@ -85,4 +67,4 @@
# 편집: Emacs 26.1 (Ubuntu 18.04)
# 최초 작성일: 2019년 1월 8일
-# 마지막 갱신: 2019년 1월 21일
+# 마지막 갱신: 2019년 1월 22일