proc

How does the “#map(&proc)” idiom work when introspecting module classes?

*爱你&永不变心* 提交于 2019-12-03 10:48:47
Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I've been able to parse bits and pieces of this, but I'm not really seeing how it all fits together. Here

Why do we need fibers

谁都会走 提交于 2019-12-03 00:02:53
问题 For Fibers we have got classic example: generating of Fibonacci numbers fib = Fiber.new do x, y = 0, 1 loop do Fiber.yield y x,y = y,x+y end end Why do we need Fibers here? I can rewrite this with just the same Proc (closure, actually) def clsr x, y = 0, 1 Proc.new do x, y = y, x + y x end end So 10.times { puts fib.resume } and prc = clsr 10.times { puts prc.call } will return just the same result. So what are the advantages of fibers. What kind of stuff I can write with Fibers I can't do

Why do we need fibers

风格不统一 提交于 2019-12-02 13:49:51
For Fibers we have got classic example: generating of Fibonacci numbers fib = Fiber.new do x, y = 0, 1 loop do Fiber.yield y x,y = y,x+y end end Why do we need Fibers here? I can rewrite this with just the same Proc (closure, actually) def clsr x, y = 0, 1 Proc.new do x, y = y, x + y x end end So 10.times { puts fib.resume } and prc = clsr 10.times { puts prc.call } will return just the same result. So what are the advantages of fibers. What kind of stuff I can write with Fibers I can't do with lambdas and other cool Ruby features? Fibers are something you will probably never use directly in

Entry in /proc/meminfo

℡╲_俬逩灬. 提交于 2019-12-02 12:29:43
问题 I am now study linux. cat /proc/meminfo produces as following. Please tell me the mean of entry "Active(file)/Inactive(file)". I can't find the explanation of these entry. Thanks. MemTotal: 7736104 kB MemFree: 166580 kB Buffers: 604636 kB Cached: 5965376 kB SwapCached: 0 kB Active: 4294464 kB Inactive: 2319240 kB Active(anon): 13688 kB Inactive(anon): 33828 kB Active(file): 4280776 kB Inactive(file): 2285412 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 16777208 kB SwapFree: 16777208 kB Dirty

Proc throws error when used with do end [duplicate]

▼魔方 西西 提交于 2019-12-02 06:52:59
问题 This question already has answers here : Code block passed to each works with brackets but not with 'do'-'end' (ruby) (3 answers) Closed 5 years ago . This doesn't work: run Proc.new do |env| [200, { "Content-Type" => "application/json; charset=UTF-8" }, ["{\"name\":\"Rack App\"}"]] end But this does: run Proc.new { |env| [200, { "Content-Type" => "application/json; charset=UTF-8" }, ["{\"name\":\"Rack App\"}"]] } Any ideas, why its throwing error when used with do..end? Error I am getting:

Proc throws error when used with do end [duplicate]

蹲街弑〆低调 提交于 2019-12-02 03:06:09
This question already has an answer here: Code block passed to each works with brackets but not with 'do'-'end' (ruby) 3 answers This doesn't work: run Proc.new do |env| [200, { "Content-Type" => "application/json; charset=UTF-8" }, ["{\"name\":\"Rack App\"}"]] end But this does: run Proc.new { |env| [200, { "Content-Type" => "application/json; charset=UTF-8" }, ["{\"name\":\"Rack App\"}"]] } Any ideas, why its throwing error when used with do..end? Error I am getting: app.ru:1:in new: tried to create Proc object without a block (ArgumentError) Your first code is interpreted as: run(Proc.new)

In Ruby is it possible to create a local variable explicitly

早过忘川 提交于 2019-12-01 21:20:56
e.g. x = 123 p = Proc.new { x = 'I do not want change the value of the outer x, I want to create a local x' } In Ruby Is there something the same as "my" keyword in Perl ? As per the Perl documentation of my ,I think you are looking for something like below in Ruby:- x = 123 p = Proc.new {|;x| x = 'I do not want change the value of the outer x, I want to create a local x' } p.call # => "I do not want change the value of the outer x, I want to create a local x" x # => 123 ChrisPhoenix Beware! (Related, though not exactly what you're asking...) The rules for variable scope changed between 1.8

how to show proccess like in ps -e

大憨熊 提交于 2019-12-01 14:52:38
Hello! I wanto to make simple c proggram what will work like ps -e. The only colums that should be shown are PID and CMD. Thats my code: #include <dirent.h> #include <errno.h> #include <sys/types.h> #include <stdio.h> #include <regex.h> int main() { DIR *dir; struct dirent *entry; if ((dir = opendir("/proc")) == NULL) perror("operation error"); else { printf("PID CMD\n"); while ((entry = readdir(dir)) != NULL) printf(" %s\n", entry->d_name); closedir(dir); } return 0; } My questins are: 1) How i can show only folders with numbers(i don't know how to implement regcomp())? 2)How to near PID

how to show proccess like in ps -e

坚强是说给别人听的谎言 提交于 2019-12-01 14:27:58
问题 Hello! I wanto to make simple c proggram what will work like ps -e. The only colums that should be shown are PID and CMD. Thats my code: #include <dirent.h> #include <errno.h> #include <sys/types.h> #include <stdio.h> #include <regex.h> int main() { DIR *dir; struct dirent *entry; if ((dir = opendir("/proc")) == NULL) perror("operation error"); else { printf("PID CMD\n"); while ((entry = readdir(dir)) != NULL) printf(" %s\n", entry->d_name); closedir(dir); } return 0; } My questins are: 1)

/proc/[pid]/cmdline file size

扶醉桌前 提交于 2019-12-01 14:10:06
i'm trying to get the filesize of the cmdline file in proc/[pid]. For example porc/1/cmdline. The file is not empty, it contains "/sbin/init". But i get file_size = 0. int main(int argc, char **argv) { int file_size; FILE *file_cmd; file_cmd = fopen("/proc/1/cmdline", "r"); if(file_cmd == NULL) { perror("proc/1/cmdline"); exit(1); }else { if(fseek(file_cmd, 0L, SEEK_END)!=0) { perror("proc/1/cmdline"); exit(1); } file_size = ftell(file_cmd); } printf("fs: %d\n",file_size); fclose(file_cmd); } Regards That's normal. /proc files (most of them, there are a few exceptions) are generated by the