简体   繁体   English

红宝石块问题循环变量

[英]ruby block questions loop variables

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. 这是来自ruby网站的一段代码。

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? 我也对他们如何读取变量名称和url感到困惑,他们是在同一行的空格中读取它们还是什么? so if I have Comics1 link_of_comics_site_1 Comics2 link_of_comics_site_2 Comics3 link_of_comics_site_3 所以如果我有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 因此对于第一次迭代,name = Comics1和url = link_of_comics_site_1

Thanks. 谢谢。

That's not just Ruby. 不只是Ruby。 That's a template for a webpage using ruby add-on methods for HTML generation. 这是使用ruby附加方法生成HTML的网页模板。

But presumably, the result of the call to load_comics is a Hash, where the keys are names and the values are URLs. 但大概是,调用load_comics的结果是一个Hash,其中的键是名称,值是URL。 You could make one of those yourself: 您可以自己做一个:

my_comics_hash = { "name1" => "url1", "name2" => "url2" }

which you can then iterate over the same way: 然后您可以通过相同的方式进行迭代:

my_comics_hash.each do |name, url|
  puts "Name #{name} goes with URL #{url}"
end

In your code, it's building up an HTML list inside a popup window, but it's the same idea. 在您的代码中,它是在弹出窗口中建立HTML列表,但这是相同的想法。 The each method 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. each方法遍历一个集合(在本例中为Hash),并对该集合中的每个项目(在本例中为每个键/值对)运行一些代码。 When you call each , you pass it a block of code inside do ... end ; 当调用each ,会在do ... 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 word do . 当前项目传递到代码块,该代码块声明一个变量,以在单词do之后紧接着将其保存在管道中。 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. 由于我们要遍历键/值对,因此可以声明两个变量,键在第一个变量中,值在第二个变量中。

In ruby function, parenthesis is optional and the ";" 在ruby函数中, 括号是可选的,而“;” end of statement is also optional. 语句结尾也是可选的。 ej j

link "click here" , "http://myweb.com" 

is equivalent to : 等效于:

link("click here", "http://myweb.com");

But If you have more than one statement in a line the ";" 但是,如果一行中有多个语句,则“;” is a must, ej 是必须的,ej

  link("click here1", "http://myweb.com"); link("click here2", "http://myweb.com");

In your code it could be written in 在你的代码中可以写成

 link(name, url)

or just 要不就

 link(name, url);

or 要么

 link name, url

But it is highly recommended to put parenthesis around function parameters for readability unless you have other reason . 但是强烈推荐在函数参数的周围加上括号以提高可读性,除非另有原因。 The ";" “;” is not common in ruby world . 在红宝石世界并不常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM