>>>>> Johann Hibschman <johann at physics.berkeley.edu> (JH) writes: JH> Steven D Majewski writes: >>> class Foo < Bar >>> def myMethod >>> File.new ("test.dat", "w") { |f| >>> f.puts "Hello world!" >>> } >>> end >>> end >> What do the vertical bars around the f ("|f|") above indicate ? JH> That indicates that the block takes one argument, which is named "f". In other words, a lambda expression. JH> In Python, this would be: In Python you could then also use the lambda expression. lambda f: f.puts("Hello World") or write rather than puts JH> def File_new (filename, func_to_call): JH> try: JH> file_obj = open (filename, 'w') JH> func_to_call (file_obj) JH> finally: JH> file_obj.close() JH> def block_function (f): # i.e. this is what that |f| means... JH> f.write ("Hello, world!\n") JH> File_new ("test.dat", block_function) What about open("test.dat", "w").write ("Hello world")? -- Piet van Oostrum <piet at cs.uu.nl> URL: http://www.cs.uu.nl/~piet [PGP] Private email: P.van.Oostrum at hccnet.nl
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4