사용자 입력을 "DD / MM / YYYY"로 수집하고 있습니다.
목표는 csv YYYY, MM, DD로 mktime에 전달하는 것입니다.
puts "Please enter dob in dd/mm/yyyy format;"
inp = gets.chomp
inp = inp.gsub(" ","")
while inp.length != 10
puts "Please use dd/mm/yyyy format"
inp = gets.chomp
end
bday = inp.gsub("/",",")
ctime = Time.new
btime = Time.mktime(bday)
lsecs = ctime - btime
ysecs = Time.mktime(2001) - Time.mktime(2000)
rsecs = 1000000000 - lsecs
ryears = rsecs / ysecs
puts "You are currently #{lsecs} seconds old"
puts "You have #{ryears} years until you are a billion seconds old!!"
보시다시피 남은 작업은 사용자 입력을 반대로하는 것이므로 컴팩트 솔루션을 찾는 데 어려움이 있습니다. 방법이 보이면이 코드를 더 짧게 만들 수 있도록 도와주세요.
솔루션 : (초와 연도를 내림 / 구분)
def reformat_date(str)
str.split("/").reverse.join(",")
end
puts "Please enter dob in dd/mm/yyyy format;"
inp = gets.chomp
inp = inp.gsub(" ","")
while inp.length != 10
puts "Please use dd/mm/yyyy format"
inp = gets.chomp
inp = inp.gsub(" ","")
end
ctime = Time.new
btime = Time.mktime(reformat_date(inp))
lsecs = ctime - btime
**lsecdel = lsecs.round(0).to_s.reverse.gsub(/...(?=.)/,'\&,').reverse**
ysecs = Time.mktime(2001) - Time.mktime(2000)
rsecs = 1000000000 - lsecs
ryears = rsecs / ysecs
puts "You are currently #{lsecdel} seconds old"
puts "You have #{ryears.round(2)} years until you are a billion seconds old!!"
이건 어떤가요?
def reformat_date(str)
str.split('/').reverse.join(',')
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다