我可以声明一个数组,其中数组元素之一是未声明的变量吗? (红宝石)

发布于 2024-08-19 00:55:52 字数 959 浏览 5 评论 0原文

#input_from_the_net = ""

my_array = [ ["Header name" , input_from_the_net] ]

input_from_the_net = "a value scraped from the net"

puts "#{my_array[0][0]} is #{my_array[0][1]}"

编辑:

我稍后在循环中使用变量 input_from_the_net 并将其值分配给哈希值。然后将该哈希值存储在另一个哈希值中。如果我使用 input_from_the_net.replace("a value scraped from the net") 它将替换所有哈希中的值。这是不希望的。我希望所有哈希值都保持正确的值。

EDIT2:更详细的示例代码

`require 'pp'
input_from_the_net = ""

def parse_the_website()
  (0..5).each { |index| 
    input_from_the_net = index+23
    @my_hash[index] = {@my_array[0][0] => input_from_the_net}
  } 
end

@my_array = [ ["Header name" , input_from_the_net] ] 
       #my_array is used on different places of the code

@my_hash = {}
parse_the_website
pp @my_hash

Q1:我可以让这个工作而不改变行的顺序吗?

Q2:如果我取消注释 #input_from_the_net = "" ,则打印时变量 input_from_the_net 的值是“”而不是“从网络中抓取的值”。怎么会?

#input_from_the_net = ""

my_array = [ ["Header name" , input_from_the_net] ]

input_from_the_net = "a value scraped from the net"

puts "#{my_array[0][0]} is #{my_array[0][1]}"

EDIT:

I use the variable input_from_the_net later on in the loop and assign its value into a hash. That hash is then stored inside another hash. If I use input_from_the_net.replace("a value scraped from the net") it replaces the value inside all hashes. That is not desired. I want all hashes to keep the correct values.

EDIT2: more detailed sample code

`require 'pp'
input_from_the_net = ""

def parse_the_website()
  (0..5).each { |index| 
    input_from_the_net = index+23
    @my_hash[index] = {@my_array[0][0] => input_from_the_net}
  } 
end

@my_array = [ ["Header name" , input_from_the_net] ] 
       #my_array is used on different places of the code

@my_hash = {}
parse_the_website
pp @my_hash

Q1: can I make this work and not change the order of lines?

Q2: if I uncomment #input_from_the_net = "" the value of the variable input_from_the_net at the time of printing is "" not the "a value scraped from the net". How come?

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

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

发布评论

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

评论(2

垂暮老矣 2024-08-26 00:55:52

您可以保持相同的顺序,但需要使用 replace

input_from_the_net = ""

my_array = [ ["Header name" , input_from_the_net] ]

input_from_the_net.replace("a value scraped from the net")

puts "#{my_array[0][0]} is #{my_array[0][1]}"

# Outputs: Header name is a value scraped from the net

每次将 = 与字符串一起使用时,它都会创建一个新字符串并将其分配给变量。要替换它而不创建新字符串,请使用 String.replace 方法。

为了好玩,您可以使用 irb 来测试一下:

>> str = "Hello"
=> "Hello"
>> str.object_id
=> 2156902220                  # original id
>> str = "New String"
=> "New String"
>> str.object_id
=> 2156889960                  # different id
>> str.replace("Third String")
=> "Third String"
>> str.object_id
=> 2156889960                  # same id

You can keep the same order you have, but you need to use replace:

input_from_the_net = ""

my_array = [ ["Header name" , input_from_the_net] ]

input_from_the_net.replace("a value scraped from the net")

puts "#{my_array[0][0]} is #{my_array[0][1]}"

# Outputs: Header name is a value scraped from the net

Every time you use = with a string, it creates a new string and assigns it to the variable. To replace it without creating a new string, you use the String.replace method.

For fun, you can use irb to test this out:

>> str = "Hello"
=> "Hello"
>> str.object_id
=> 2156902220                  # original id
>> str = "New String"
=> "New String"
>> str.object_id
=> 2156889960                  # different id
>> str.replace("Third String")
=> "Third String"
>> str.object_id
=> 2156889960                  # same id
伤痕我心 2024-08-26 00:55:52

我建议创建一个简单的类:

class Netinput
  attr_accessor :netinput

def initialize(netinput = "")
  @netinput = netinput
end

end

然后像这样使用它:

input_from_the_net = "our test string"

#Netinput can take a string or no argument
my_array = [ ["Header name" , Netinput.new()] ]

my_array[0][1].netinput = input_from_the_net

puts "#{my_array[0][0]} is #{my_array[0][1].netinput}"

# Outputs: Header name is our test string

通过这种方法,每个数组的第二个元素是一个具有自己的实例变量“netinput”的对象。希望这对你有用。

I recommend making a simple class:

class Netinput
  attr_accessor :netinput

def initialize(netinput = "")
  @netinput = netinput
end

end

Then use it like this:

input_from_the_net = "our test string"

#Netinput can take a string or no argument
my_array = [ ["Header name" , Netinput.new()] ]

my_array[0][1].netinput = input_from_the_net

puts "#{my_array[0][0]} is #{my_array[0][1].netinput}"

# Outputs: Header name is our test string

With this approach the second element of each array is an object that has its own instance variable "netinput." Hopefully this works for you.

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