如何在 SketchUp Ruby 中调用函数外部的数组

发布于 2025-01-09 02:02:30 字数 1023 浏览 0 评论 0原文

我正在构建一个自定义十进制度 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-16 02:02:30

您的第二个代码块是错误的,如果您运行它,您会得到 main:Object 的未定义局部变量或方法数组

您可能正在交互式会话中运行代码,并且之前已经定义了 array,请考虑到 arrayDMS 函数的本地函数。

我想说你想要做的是

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

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 account array is local to DMS function.

I would say what you want is to do

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)

Assigning the output of DMS to bearingarray

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文