views:

88

answers:

2
#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?

+5  A: 

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
Doug Neiner
that's interesting, I didn't know that about strings. And is there any way not to use `input_from_the_net = ""` at all?
Radek
You need to assign it as a string at some point, but you can do it inline: `my_array = [ ["Header name" , input_from_the_net = ""] ]` and save the previous call to initialize the variable.
Doug Neiner
@Doug Neiner: the inline initialization is great.
Radek
@Doug Neiner: I cannot use the .replace method. See my update in my question. I replaces all values 'backwards'.
Radek
+1  A: 

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.

Beanish
@Beanish: your answer is closer to something I am looking for. Is that possible to change the code that the line `input_from_the_net = "our test string"` won't be there and that it would follow the code structure from my question under Edit2 section? I tried that I am getting `undeclared array with object.rb:6: uninitialized constant Netinput (NameError)`. What is really important that I can use the array inside functions/procedures.
Radek
you don't need the line input_from_the_net = "our test string" that is just setting a value to that variable, however you set it I don't know. If you remove that line my code still works because I set the instance variable to "" if no argument is given. If you copy and paste my two code blocks into a file and run it, it should work for you. Then modify that to fit your means.
Beanish
@Beanish: yes, your initial code is working fine.When modifying it I moved `my_array = [ ["Header name" , Netinput.new()] ]` before the definition of the class thinking that the order doesn't matter. Very nice solution.Thank you.
Radek