如何在 SketchUp Ruby 中调用函数外部的数组
我正在构建一个自定义十进制度 (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] 数组。
我的期望是我会在数组中获得完全相同的值,但名称不同。
出了什么问题?我应该做什么来修复它?
感谢您的帮助!
I am building a custom Decimal Degrees (DD) to Decimal Minute Seconds (DMS) function to use in SketchUp's Ruby. Below is my script.
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)
So far so good, if you ran this script in Ruby, you'd probably end up with an array that gives you
[45, 31, 30.44]
I then try to add a line of code which assigns that array under a different name. Here is the new code with the extra line.
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
If you ran the second block of code however, you would end up with an array of [1, 2, 3].
My expectation was that I would get that exact same values in the array, but under the different name.
What has gone wrong? What should I do to fix it?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二个代码块是错误的,如果您运行它,您会得到
main:Object 的未定义局部变量或方法数组
。您可能正在交互式会话中运行代码,并且之前已经定义了
array
,请考虑到array
是DMS
函数的本地函数。我想说你想要做的是
将
DMS
的输出分配给bearingarray
Your second code block is wrong, if you run it you get
undefined local variable or method array for main:Object
.You are probably running the code in an interactive session and you have defined
array
before, take into accountarray
is local toDMS
function.I would say what you want is to do
Assigning the output of
DMS
tobearingarray