Hommer Smith
Hommer Smith

Reputation: 27852

How to share a variable that is set inside a block

I have a method that does the following (using open-uri)

def convert_html(remote_url)
  open(remote_url) do |file|
    # After some file manipulation, I do an assignment
    rendered_html = find_html_to_render
  end
  # How can I access rendered_html here??
end

I would like to know how I can access rendered_html once the IO operations with open have finished.

Upvotes: 0

Views: 25

Answers (1)

sawa
sawa

Reputation: 168091

Initialize the variable outside of the block.

def convert_html(remote_url)
  rendered_html = nil
  open(remote_url) do |file|
    # After some file manipulation, I do an assignment
    rendered_html = find_html_to_render
  end
  ...
end

Upvotes: 3

Related Questions