0

While it's a terrible idea to call a method that is entered by the user, but in language like Ruby you can call method() on an Object using a.method(), but you can also use send(method_name, other_object), for example:

$ ruby -e "p '1234abc'.send(:to_i)"
1234

ruby -e "puts '1234ab'.method(:to_i).call"
1234

Another Example:

ruby -e "n1, n2 = 5, 6 ; puts %i(+ - * / fdiv).map { |x| %[#{n1} #{x} #{n2} = #{n1.send(x, n2)}] }"
5 + 6 = 11
5 - 6 = -1
5 * 6 = 30
5 / 6 = 0
5 fdiv 6 = 0.8333333333333334

Another example:

$ ruby -e "puts ; n = ->n { print %(Enter #{n}: ) ; STDIN.gets.strip } ; num1 = n.('first value') ; num2 = n.('second value') ; meth = n.('method name') ; puts num1.to_i.send(meth, num2.to_i)"

Enter first value: 5
Enter second value: 10
Enter method name: *
50

$ ruby -e "puts ; n = ->n { print %(Enter #{n}: ) ; STDIN.gets.strip } ; num1 = n.('first value') ; num2 = n.('second value') ; meth = n.('method name') ; puts num1.to_i.send(meth, num2.to_i)"

Enter first value: 50 
Enter second value: 20  
Enter method name: fdiv
2.5

Can I do the same with JS?

For example:

var foo = '+'
1.call(foo, 2)    # => 3
8.call('*', 5)    # => 40
'Hello World'.call(split, ' ')    # => ["Hello", "World"]
15 Volts
  • 1,946
  • 15
  • 37
  • maybe something like [`call()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) ? – fedesc Aug 02 '20 at 09:03
  • I have a variable `foo = '+'` ; and I want to call it on 1 with 2, say `1.call(foo, 2)`, instead of 1 + 2... – 15 Volts Aug 02 '20 at 09:06
  • You can - and as you realise you probably shouldn't: let functionName = "printMethod"; eval(functionName + '("from eval call")'); function printMethod(from){ console.log("Hello World " + from); } – Mark Taylor Aug 02 '20 at 09:10
  • `+` is not a method, it is an operator. – Titus Aug 02 '20 at 09:10
  • Yes, JS by nature is a dynamic language. So `object["someMethod"](params)` would call a method called `someMethod` on object. But be aware `this` context is not automatically bound when doing this, that's what call and bind are used for. So you might want `object["someMethod"].call(object, params)` – Keith Aug 02 '20 at 09:13
  • You could define methods for each operation (eg: `var operations = { '+': (n1, n2) => (n1 + n2) }`) and then call the appropriate function using the user input (eg: `operations[x](n1, n2)`) – Titus Aug 02 '20 at 09:15
  • Hi @keith, `js> 1['+'](54) typein:9:3 TypeError: 1['+'] is not a function Stack: @typein:9:3 ` Same with `.call(param)`, Using SpiderMonkey `JavaScript-C73.0` – 15 Volts Aug 02 '20 at 09:19
  • Not sure what I am doing wrong, but so far https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable doesn't actually solve my question... – 15 Volts Aug 02 '20 at 09:30
  • 1
    It doesn't because that relates to accessing properties of an object using a **string**. In **JavaScript**, `+` is an operator not a function, there is no equivalent for the `ruby` approach, look at my previous comment for an alternative. – Titus Aug 02 '20 at 09:35
  • Oh I see, can't really do anything like that... – 15 Volts Aug 02 '20 at 09:35

0 Answers0