Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

do |###| statements

I have absolutely no clue as to how to use the do |###| statements. I think that they work a little along the lines of this:
Code:
File.foreach do |pie|
pie << "Hello World!"
end

But am not sure. Clarification would be much appreciated.
 

khmp

Sponsor

".foreach" seems to be an IO specific class method. If you are using a file you can just use, "each" which has the same functionality. In any case both are iterating through each individual line in the file. Specifically used for reading a file. Each line is seperated by a newline I believe. "pie" becomes the line you are currently looking at while iterating through the file. However using the << operator will append "Hello World!" to "pie". "Pie" is only a copy of it so it won't effect the file. So if the first line in the file was the string "This is the first line". With that code after the << operator "pie" will be "This is the first line\nHello Word!".

Now as to the actual "do |iterator(s)|" I'm not exactly sure about wording. To me "do" proceeds an "each" of some kind, and the object inside the || represents a copy of a part of whatever precedes the "each".

For example:
Ruby:
Code:
loaf_of_bread.each do |slice|
  # do whatever with the slice of bread.
end

However if your code is concise placement can be done on one line and you can omit the "do":
Code:
loaf_of_bread.each {|slice| p slice + "was delicious." }

I hope that helps. If you any other questions feel free to ask.

Good luck with it Lorem Ipsum! :thumb:
 
khmp":3478hbr1 said:
Now as to the actual "do |iterator(s)|" I'm not exactly sure about wording. To me "do" proceeds an "each" of some kind, and the object inside the || represents a copy of a part of whatever precedes the "each".

The "do" just marks the start of a block. do ... end is the same as { ... }, except that you can use more than one line in a do block. If you want to do that with a curly block you need semi-colons on the end of each line. You can think of blocks as like anonymous functions that get called once for each element in  the list/file/whatever.

Incidentally, there are a load of methods that take blocks. Array has a pile of them, not all of them mentioned in the RMXP docs.  Want to select all the even numbers from a list?

Code:
[1,2,3].select { |i| i % 2 == 0}

That returns a new array. Every element passed to the block in the "i" variable, and if the block evaluates true, it gets copied into the new array. Otherwise it gets left out.

There's a reject version too. I'll do this one with do ... end.
Code:
[1,2,3].reject do |i|
      i % 2 == 0
end

suppose you have a list of Item IDs and you want a list of item names? Use map:

ids = [2,5,7,14]
names = ids.map { |id| $data_items[id].name }

And each ID in the list gets replaced with the name from the item array

The bit in between the |pipe symbols| is sometimes called a "bound variable". If the block  gets called once for each item in an array, then the bound variable is set to item being evaluated for each call. Just like a function argument.

And like a function, you can have more than one parameter: try running each on a Hash, for instance:

Code:
h = {"a"=>1, "b"=>2, "c" => 3}
h.each do |k,v|
        print "key = #{k}, value = #{v}\n"
end

Hope that helps - it can be a tricky concept to get your head around
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top