comics = load_comics( '/comics.txt' )
Popup.make do
h1 "Comics on the Web"
list do
comics.each do |name, url|
link name, url
end
end
end
I am new to ruby. This is a piece of code from a ruby website.
I cant find what ‘link’ and ‘list’ keyword in the menu.
can someone explain it a little bit those two keywords, and where is the definition of those two keyword .
I am also confused on how they read the variables name and url, they are reading it by the space at the same line or what?
so if I have
Comics1 link_of_comics_site_1
Comics2 link_of_comics_site_2
Comics3 link_of_comics_site_3
so for the first iteration, name=Comics1, and url =link_of_comics_site_1
Thanks.
That’s not just Ruby. That’s a template for a webpage using ruby add-on methods for HTML generation.
But presumably, the result of the call to
load_comicsis a Hash, where the keys are names and the values are URLs. You could make one of those yourself:which you can then iterate over the same way:
In your code, it’s building up an HTML list inside a popup window, but it’s the same idea. The
eachmethod iterates over a collection – in this case a Hash – and runs some code on every item in that collection – in this case, each key/value pair. When you calleach, you pass it a block of code insidedo…end; that’s the code that gets run on each item. The current item is passed to the code block, which declares a variable to hold it inside the pipes right after the worddo. Since we’re iterating over key/value pairs, we can declare two variables, and the key goes in the first and the value in the second.