我正在構建一個自定義十進制度 (DD) 到十進制度 (DMS) 函式以在 SketchUp 的 Ruby 中使用。下面是我的腳本。
arg1 = 45.525123
def DMS(arg1)
angle = arg1
deg = angle.truncate()
dec = (angle - angle.truncate()).round(6)
totalsecs = (dec * 3600).round(6)
mins = (totalsecs / 60).truncate()
secs = (((totalsecs / 60) - (totalsecs / 60).truncate()) * 60).round(2)
array = [deg, mins, secs]
end
DMS(arg1)
到目前為止一切順利,如果你在 Ruby 中運行這個腳本,你最終可能會得到一個陣列,為你提供 [45, 31, 30.44]
然后我嘗試添加一行代碼,以不同的名稱分配該陣列。這是帶有額外行的新代碼。
arg1 = 45.525123
def DMS(arg1)
angle = arg1
deg = angle.truncate()
dec = (angle - angle.truncate()).round(6)
totalsecs = (dec * 3600).round(6)
mins = (totalsecs / 60).truncate()
secs = (((totalsecs / 60) - (totalsecs / 60).truncate()) * 60).round(2)
array = [deg, mins, secs]
end
DMS(arg1)
bearingarray = array
然而,如果你運行第二個代碼塊,你最終會得到一個 [1, 2, 3] 的陣列。
我的期望是我會在陣列中得到完全相同的值,但名稱不同。
出了什么問題?我應該怎么做才能修復它?
謝謝你的幫助!
uj5u.com熱心網友回復:
你的第二個代碼塊是錯誤的,如果你運行它,你會得到undefined local variable or method array for main:Object.
您可能正在互動式會話中運行代碼,并且您之前已經定義array過,考慮到array本地DMS功能。
我會說你想做的是
arg1 = 45.525123
def DMS(arg1)
angle = arg1
deg = angle.truncate()
dec = (angle - angle.truncate()).round(6)
totalsecs = (dec * 3600).round(6)
mins = (totalsecs / 60).truncate()
secs = (((totalsecs / 60) - (totalsecs / 60).truncate()) * 60).round(2)
array = [deg, mins, secs]
end
bearingarray = DMS(arg1)
將輸出分配DMS給bearingarray
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431895.html
