简体   繁体   English

如何在SketchUp Ruby中调用function之外的数组

[英]How to call an array outside of a function in SketchUp Ruby

I am building a custom Decimal Degrees (DD) to Decimal Minute Seconds (DMS) function to use in SketchUp's Ruby. Below is my script.我正在构建自定义十进制度 (DD) 到十进制分秒 (DMS) function,以在 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)

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]到目前为止一切顺利,如果您在 Ruby 中运行此脚本,您最终可能会得到一个数组 [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].但是,如果您运行第二个代码块,您将得到一个 [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!谢谢你的帮助!

Your second code block is wrong, if you run it you get undefined local variable or method array for main:Object .你的第二个代码块是错误的,如果你运行它,你会得到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.您可能正在交互式 session 中运行代码并且您之前已经定义了array ,考虑到array对于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 bearingarrayDMS的output赋值给bearingarray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM