在 Ruby 中表达此语句的正确方式是什么?

发布于 2024-08-22 18:48:34 字数 926 浏览 6 评论 0原文

我对此很陌生,并且很难找到用 Ruby 表达此内容的正确方法。而且我不知道SketchUp中的Ruby API是否有所不同。但是,这就是我想用它的目的。

def self.initialize_job_info
    return{
        'salesperson' => ' = $pg_settings['salespersons'[['salesperson']['id']]] if ('on' = $pg_settings['salespersons'[['salesperson']['defsales']]])'

这就是我基本上想做的:

这部分代码按其应有的方式工作

def self.initialize_job_info
    return{
        'salesperson' => ''

如果没有找到预先存在的值,它会将空表单的 job_info['salesperson'] 值的初始值设置为“ ”。

因此,我想在从 $pg_settings 传递的哈希中放置一个值。

我想要的值是,并且我希望这是有意义的,这个特定“id”的值

$pg_settings['salespersons'] {//which is a list of 'salesperson'
    <salesperson> id="561" name="name" phone="phone number" defsales="on" email="email" </salesperson>

if (defsales == "on") then 'salesperson' => 'value="id"'

这有意义吗?

我正在拉扯我的头发,所以你能在这方面提供的任何帮助都会很棒。

I'm new at this, and I'm having trouble finding the proper way to phrase this in Ruby. And I don't know if the Ruby API in SketchUp is different. But, thats what I'm trying to use it for.

def self.initialize_job_info
    return{
        'salesperson' => ' = $pg_settings['salespersons'[['salesperson']['id']]] if ('on' = $pg_settings['salespersons'[['salesperson']['defsales']]])'

This is what I'm basically trying to do:

This part of the code works as it should

def self.initialize_job_info
    return{
        'salesperson' => ''

It sets an empty form's initial value of job_info['salesperson']'s value to ' ' if no pre-existing value is found.

So, there is a value I want to place in the Hash that is being passed from $pg_settings.

The value I want is, and I hope this make sense, the value of this specific 'id'

$pg_settings['salespersons'] {//which is a list of 'salesperson'
    <salesperson> id="561" name="name" phone="phone number" defsales="on" email="email" </salesperson>

if (defsales == "on") then 'salesperson' => 'value="id"'

Does this make sense?

I'm pulling my hair out, so any help you can give on this would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

極樂鬼 2024-08-29 18:48:34

如果那些不在引号内的名称是您想要从中获取值的变量,则可能应该是:

'salesperson' => " = $pg_settings[#{salespersons}[[#{salesperson}][#{id}]]] if (#{on} = $pg_settings[#{salespersons}[[#{salesperson}][#{defsales}]]])"

但正如 Geo 所说,有关实际目的/意图的更多详细信息将有助于

顺便说一句,该构造称为字符串插值(http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation)

if those names not inside the quotes are variables that you want to get the values from it should probably be:

'salesperson' => " = $pg_settings[#{salespersons}[[#{salesperson}][#{id}]]] if (#{on} = $pg_settings[#{salespersons}[[#{salesperson}][#{defsales}]]])"

but as Geo said, more detail on the actual purpose/intent would help

BTW, that construc tis called string interpolation (http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation)

静赏你的温柔 2024-08-29 18:48:34

如果您想插入字符串,例如将它们的值包含在字符串中,那么也许这个示例可以帮助您:

a = "a string"
b = "this is"
c = "#{b} #{a}"

在上面的示例中,c 将具有值:this is a string 。此外,在插值时,会接受有效的 Ruby 代码。所以,这也可以:

c = "#{ b.sub("this","") } #{a}"

在这种情况下, c 将具有值 is a string 。因此,如果您需要插入某些内容,请首先考虑如何使用普通代码来执行此操作,然后只需在其周围添加 #{} 即可。

If you want to interpolate the strings, as in include their value in a string, then maybe this examples can help you:

a = "a string"
b = "this is"
c = "#{b} #{a}"

In the example above, c will have the value: this is a string . Also, while interpolating, valid Ruby code is accepted. So, this is ok too:

c = "#{ b.sub("this","") } #{a}"

And in this case, c will have the value is a string . So, if you need to interpolate something, first think about how you would do it using normal code, and then just add #{} around it.

我乃一代侩神 2024-08-29 18:48:34

我想通了。

这是工作代码

def self.initialize_job_info
    return{
        'salesperson' => self.default,
    }
end

def self .default
    salespersons = $pg_settings['salespersons']
    salespersons.each do |salesperson|

    if (salesperson['defsales'] == 'on')
        return salesperson['id']
        end
    end
end

看起来我还有很长的路要走.......哈哈

I figured it out.

Here is the working code

def self.initialize_job_info
    return{
        'salesperson' => self.default,
    }
end

def self .default
    salespersons = $pg_settings['salespersons']
    salespersons.each do |salesperson|

    if (salesperson['defsales'] == 'on')
        return salesperson['id']
        end
    end
end

Looks like I was a long way off.......lol

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