methods

Obtaining number of block parameters

拥有回忆 提交于 2019-12-21 03:59:07
问题 I need to obtain the number of parameters a given block takes. For example: foobar(1,2,3) { |a, b, c| } def foobar(x, y, z, &block) # need to obtain number of arguments in block # which would be 3 in this example end This is possible in the 1.9 trunk, but not in any official release. I was hoping if there's any way to do this without having to download a separate gem/extension module. 回答1: When you materialize a block with &, it becomes a Proc object, which has an arity method. Just be

In javascript, how do I call a class method from another method in the same class?

雨燕双飞 提交于 2019-12-21 03:57:37
问题 I have this: var Test = new function() { this.init = new function() { alert("hello"); } this.run = new function() { // call init here } } I want to call init within run. How do I do this? 回答1: Use this.init() , but that is not the only problem. Don't call new on your internal functions. var Test = new function() { this.init = function() { alert("hello"); }; this.run = function() { // call init here this.init(); }; } Test.init(); Test.run(); // etc etc 回答2: Instead, try writing it this way:

What is the easiest way to generate a Control Flow-Graph for a method in Python?

时光毁灭记忆、已成空白 提交于 2019-12-21 03:37:14
问题 I am writing a program that tries to compare two methods. I would like to generate Control flow graphs (CFG) for all matched methods and use either a topological sort to compare the two graphs. 回答1: RPython, the translation toolchain behind PyPy, offers a way of grabbing the flow graph (in the pypy/rpython/flowspace directory of the PyPy project) for type inference. This works quite well in most cases but generators are not supported. The result will be in SSA form, which might be good or bad

How do I change file extension with javascript

一个人想着一个人 提交于 2019-12-21 03:28:07
问题 Does anyone know an easy way to change a file extension in Javascript? For example, I have a variable with "first.docx" but I need to change it to "first.html". 回答1: This will change the string containing the file name; file = file.substr(0, file.lastIndexOf(".")) + ".htm"; For situations where there may not be an extension: var pos = file.lastIndexOf("."); file = file.substr(0, pos < 0 ? file.length : pos) + ".htm"; 回答2: file = file.replace(/\.[^.]+$/, '.html'); 回答3: In Node.js: path.join

Cannot Assign because it is a method group C#?

吃可爱长大的小学妹 提交于 2019-12-21 03:22:29
问题 Cannot Assign "AppendText" because it is a "method group". public partial class Form1 : Form { String text = ""; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { String inches = textBox1.Text; text = ConvertToFeet(inches) + ConvertToYards(inches); textBox2.AppendText = text; } private String ConvertToFeet(String inches) { int feet = Convert.ToInt32(inches) / 12; int leftoverInches = Convert.ToInt32(inches) % 12; return (feet + " feet and " +

Why don't methods of structs have to be declared in C++?

筅森魡賤 提交于 2019-12-21 03:11:17
问题 Take, for example, the following code: #include <iostream> #include <string> int main() { print("Hello!"); } void print(std::string s) { std::cout << s << std::endl; } When trying to build this, I get the following: program.cpp: In function ‘int main()’: program.cpp:6:16: error: ‘print’ was not declared in this scope Which makes sense. So why can I conduct a similar concept in a struct, but not get yelled at for it? struct Snake { ... Snake() { ... addBlock(Block(...)); } void addBlock(Block

Call “(id)sender” method in Xcode

ε祈祈猫儿з 提交于 2019-12-21 02:46:29
问题 Here is the method I wish to call: - (void)myMethod:(id)sender { How would I call it? I tried: [self myMethod] ^ error: Expected Expression before "]" token. I know this is a simple question, but I am new to iPhone Development 回答1: The method takes one parameter, so you have to give it one. If you have no sender you want to give, just pass nil: [self myMethod:nil]; You could also overload the method as a convenience: // declarations - (void)myMethod; - (void)myMethod:(id)sender; //

Objective c implement method which takes array of arguments

六眼飞鱼酱① 提交于 2019-12-21 02:26:25
问题 Hee Does anybody know how to implement an method in objective c that will take an array of arguments as parameter such as: [NSArray arrayWithObjects:@"A",@"B",nil]; The method declaration for this method is: + (id)arrayWithObjects:(id)firstObj... I can't seem to make such method on my own. I did the following: + (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes; [SomeClass doSometing:@"A",@"B",nil manyTimes:2]; It will give the warningtoo many arguments to function 'doSometing

Example of an instance method? (Java)

怎甘沉沦 提交于 2019-12-20 23:27:35
问题 I'm still learning about methods in Java and was wondering how exactly you might use an instance method. I was thinking about something like this: public void example(String random) { } However, I'm not sure if this is actually an instance method or some other type of method. Could someone help me out? 回答1: If it's not a static method then it's an instance method. It's either one or the other. So yes, your method, public void example(String random) { // this doesn't appear to do anything } is

What is the difference between a delegate instance and a method pointer?

浪子不回头ぞ 提交于 2019-12-20 20:21:38
问题 I thought that a delegate instance was interchangeable with a function instance. Take the following code: delegate int AddDelegate(int a, int b); AddDelegate DelegateInstance; public void DoStuff() { //I can call this without a delegate "instance": MethodThatTakesAdd(Add); //I can also call it WITH a delegate "instance" DelegateInstance = Add; MethodThatTakesAdd(DelegateInstance); } public int Add(int a, int b) { return a + b; } public void MethodThatTakesAdd(AddDelegate addFunction) {