So basically what I am doing is creating a list of URLs that I am parsing from a specific url…in this case, the base url is the local variable url.
What I want to do is, pass url to my_list – which works fine now…but once I have that list, I then want to pass the first element of the array list declared in the method my_list to the method add_category.
See the code:
def add_category(url)
new_url = url + '/web'
end
url = 'http://www.someurl.com'
def my_list(url)
root = Nokogiri::HTML(open(url))
list = root.css("a").map do |link|
[link.text, link[:href]]
end
end
my_list(url)
add_category(list[0])
Where I am going is, I want to modify every single URL in the list array per the rules I specify in the add_category method.
Thoughts?
You cannot refer to method’s local variables from outside. They are called “local” for a reason! 🙂
It seems that your
my_listmethod is simply generating a list that you want to use later. So, save its return value to a local variable (in the caller scope) and use however you want. Check this out: