"Ruby" (External Evaluation System)
Details
- Ruby Version 2.0 and higher is supported.
- To configure Ruby for use in the Wolfram Language, follow the instructions from the Configure Ruby for ExternalEvaluate workflow.
ExternalEvaluate Usage
- ExternalEvaluate["Ruby",code] executes the code string in a Ruby REPL and returns the results as a Wolfram Language expression.
- ExternalEvaluate["Ruby""String",code] executes the code string in a Ruby REPL and returns the output as a Wolfram Language string.
- Possible settings for "type" in ExternalEvaluate["Ruby""type",code] include:
-
"Expression" attempt to convert to a Wolfram Language expression "String" give the raw string output by the external evaluator "ExternalObject" return the result as ExternalObject
Data Types
- The following Ruby built-in types are supported:
-
Array List array of values BigDecimal Real arbitrary-precision real number Bignum Integer arbitrary-sized integer boolean True|False Boolean values Complex Complex complex number Fixnum Integer machine-size number Float Real real-valued number Hash Association associative array nil Null null value Rational Rational rational number String String sequence of character values
Supported External Operations
- ExternalOperation["Eval","code"] represents an external evaluation of "code".
- ExternalOperation["Eval","code",assoc] represents an external evaluation of "code" with parameters given by assoc.
- ExternalOperation["Call",func,arg1,arg2,…] calls the function func with the given arguments arg1, arg2, ….
- ExternalOperation["GetAttribute",obj,"attr"] gets the attribute "attr" of obj.
- ExternalOperation["SetAttribute",obj,"attr",val] sets the attribute "attr" of obj to the given value val.
- ExternalOperation["Cast",obj,"type"]casts obj to the given "type".
Examples
open all close allBasic Examples (3)
Evaluate 2+2 in Ruby and return the result:
ExternalEvaluate["Ruby", "2+2"]ExternalEvaluate["Ruby", "[1,2,3,4]"]Type > and select Ruby from the drop-down menu to get a code cell that uses ExternalEvaluate to evaluate:
Scope (27)
session = StartExternalSession["Ruby"]ExternalEvaluate[session, "true || false"]Concatenate strings in Ruby and return the result:
ExternalEvaluate[session, "\"hello\" + \" \" + \"world\""]Hashes in Ruby are returned as associations:
ExternalEvaluate[session, "{\"one\"=>1, \"two\"=>2, \"three\"=>3}"]DeleteObject[session]Session Options (8)
"ReturnType" (3)
For the Ruby evaluation system, the default return type is "Expression":
ExternalEvaluate["Ruby", "2+2"] === ExternalEvaluate["Ruby" -> "Expression", "2+2"]Numbers, strings, lists and associations are automatically imported for the "Expression" return type:
ExternalEvaluate["Ruby", "[1, 2, 3]"]MatchQ[%, {__Integer}]The return type of "String" returns a string of the result by calling the Ruby method to_s:
ExternalEvaluate["Ruby", "[1, 2, \"א\"]"]ExternalEvaluate["Ruby" -> "String", "[1, 2, \"א\"]"]ExternalEvaluate["Ruby", "([1, 2, \"א\"]).to_s"]"Evaluator" (1)
"SessionProlog" (1)
"SessionEpilog" (1)
"Prolog" (1)
Command Options (10)
"Command" (3)
When only a string of Ruby code is provided, the command is directly executed:
ExternalEvaluate["Ruby", "(1..5).to_a"]The above is equivalent to writing the command using this form:
ExternalEvaluate["Ruby", <|"Command" -> "(1..5).to_a"|>]Use a File wrapper to run the code in a file:
file = Export[CreateFile[], "def add2(number)
number + 2
end", "Text"]ExternalEvaluate["Ruby", {
<|"Command" -> File[file]|>,
<|"Command" -> "add2(3)"|>
}]The above is equivalent to writing the command using this form:
ExternalEvaluate["Ruby", {
File[file],
"add2(3)"
}]Put code in a CloudObject:
cloudObj = CloudExport["print('Hello World!')", "Text", CloudObject["hello-world-rb"]]Evaluate directly from the cloud:
ExternalEvaluate["Ruby", <|"Command" -> cloudObj|>];The above is equivalent to writing the command using this form:
ExternalEvaluate["Ruby", cloudObj];"ReturnType" (1)
By default, the command is executed using the "ReturnType" specified during the session creation:
ExternalEvaluate[
{"Ruby", "ReturnType" -> "String"},
"{'a'=> 2}"
]Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:
ExternalEvaluate[
{"Ruby", "ReturnType" -> "String"},
{
<|"Command" -> "{'a'=> 2}"|>,
<|"Command" -> "{'a'=> 2}", "ReturnType" -> "Expression"|>
}
]"Arguments" (2)
Use "Arguments" to call a Ruby function with arguments:
ExternalEvaluate[
"Ruby",
<|"Command" -> "Proc.new {|x,y| x+y}", "Arguments" -> {2, 3}|>
]When a non-list argument is provided, a single argument is passed to the function:
ExternalEvaluate[
"Ruby",
<|"Command" -> "Proc.new {|x| x+3}", "Arguments" -> 2|>
]If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:
ExternalEvaluate[
"Ruby",
{
<|"Command" -> "Proc.new {|x| x}", "Arguments" -> 1|>,
<|"Command" -> "Proc.new {|x| x}", "Arguments" -> {1}|>,
<|"Command" -> "Proc.new {|x| x}", "Arguments" -> {{1}}|>
}
]You can define a function inside "Command" and directly call it with "Arguments":
ExternalEvaluate[
"Ruby",
<|"Command" -> "lambda {|x, y| x+y}", "Arguments" -> {3, 5}|>
]The same result can be achieved by using a Rule:
ExternalEvaluate[
"Ruby",
"lambda {|x, y| x+y}" -> {1, 3}
]You can also pass arguments by creating an ExternalFunction:
session = StartExternalSession["Ruby"]function = ExternalFunction[session, "lambda {|x, y| x+y}"]function[1, 3]DeleteObject[session]"Constants" (1)
"TemplateArguments" (3)
When running a command, you can inline a TemplateExpression:
x = RandomReal[]
ExternalEvaluate["Ruby", "1 + <* x *>"]You can explicitly fill TemplateSlot using "TemplateArguments":
ExternalEvaluate[
"Ruby",
<|"Command" -> "2 + ``", "TemplateArguments" -> 1|>
]When a non-list argument is provided, a single template argument is passed to the template:
ExternalEvaluate[
"Ruby",
<|"Command" -> "(``).to_s", "TemplateArguments" -> 5|>
]If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:
ExternalEvaluate[
"Ruby",
{
<|"Command" -> "(``).to_s", "TemplateArguments" -> 1|>,
<|"Command" -> "(``).to_s", "TemplateArguments" -> {2}|>,
<|"Command" -> "(``).to_s", "TemplateArguments" -> {{3}}|>
}
]You can name template slots and use an Association to pass named arguments to the template:
ExternalEvaluate[
"Ruby",
<|"Command" -> "(`start`.. `end`).to_a", "TemplateArguments" -> <|"start" -> 0, "end" -> 10|>|>
]External Operations (8)
"Eval" (1)
Run an ExternalOperation that represents arbitrary code evaluation in Python:
ExternalEvaluate["Ruby", ExternalOperation["Eval", "2+2"]]Use the second argument to pass an evaluation context:
ExternalEvaluate["Ruby", ExternalOperation["Eval", "a + b", <|"a" -> 2, "b" -> 3|>]]"Call" (3)
Define an ExternalOperation that creates a function in Ruby:
op = ExternalOperation["Eval", "Proc.new { |a, b| a + b }"]Call the function by running the ExternalOperation "Call":
call = ExternalOperation["Call", op, 1, 2]Run the operation using ExternalEvaluate:
ExternalEvaluate["Ruby", call]Any argument of the "Call" operation can be an ExternalOperation:
ExternalEvaluate["Ruby", ExternalOperation["Call", op, 1, ExternalOperation["Eval", "2+2"]]]Arguments can also be passed directly in ExternalEvaluate by doing the following:
ExternalEvaluate["Ruby", op -> {1, 2}]The result is equivalent to running the following Ruby code:
ExternalEvaluate["Ruby", "f = Proc.new { |a, b| a + b }.call(1, 2)"]Create an ExternalFunction for a Ruby function:
func = ExternalFunction["Ruby", "Proc.new { |a, b| a + b }"]Call the function by running the operation "Call":
ExternalEvaluate["Ruby", ExternalOperation["Call", func, 1, 2]]The same result can be achieved by doing the following:
ExternalEvaluate["Ruby", func -> {1, 2}]Create an ExternalObject for a function in Ruby:
session = StartExternalSession["Ruby"]func = ExternalEvaluate[session, <|"Command" -> "Proc.new { |a, b| a + b }", "ReturnType" -> "ExternalObject"|>]Call the function by running the operation "Call":
ExternalEvaluate[session, ExternalOperation["Call", func, 1, 2]]The same result can be achieved by doing the following:
ExternalEvaluate[session, func -> {1, 2}]Or by using ExternalObject subvalues:
func[ExternalOperation["Call", 1, 2]]DeleteObject[session]"GetAttribute" (2)
Start a Ruby session to work with dates:
session = StartExternalSession[{"Ruby", "SessionProlog" -> "require 'date'"}];Return an ExternalObject for a Date object:
now = ExternalEvaluate[session, <|"Command" -> "Date.new(2022, 10, 10)", "ReturnType" -> "ExternalObject"|>]Extract the year attribute by using "GetAttribute":
ExternalEvaluate[session, ExternalOperation["GetAttribute", now, "year"]]The result is equivalent to running the following Ruby code:
ExternalEvaluate[session, "Date.new(2022, 10, 10).year"]DeleteObject[session]Create an ExternalObject that represents a date:
session = StartExternalSession[{"Ruby", "SessionProlog" -> "require 'date'"}]now = ExternalEvaluate[session, <|"Command" -> "Date.new(2022, 10, 10)", "ReturnType" -> "ExternalObject"|>]Use ExternalOperation to get the current year:
now[ExternalOperation["GetAttribute", "year"]]For most evaluators, "GetAttribute" is the default operation, and ExternalOperation can be omitted:
now["year"]DeleteObject[session]"SetAttribute" (1)
Start a Ruby session to work with dates:
session = StartExternalSession[{"Ruby", "SessionProlog" -> "require 'date'"}];date = ExternalEvaluate[session, <|"Command" -> "Date.new(2022, 10, 10)", "ReturnType" -> "ExternalObject"|>]ExternalEvaluate[session, ExternalOperation["SetAttribute", date, "year", 2024]]Check that the year was set to 2024:
ExternalEvaluate[session, ExternalOperation["GetAttribute", date, "year"]]The result is equivalent to running the following Ruby code:
ExternalEvaluate[session, "d = Date.new(2022, 10, 10); d.instance_variable_set('@year', 2024)"]DeleteObject[session]"Cast" (1)
Create an ExternalObject that represents the current date:
session = StartExternalSession["Ruby"];now = ExternalEvaluate[session, <|"Command" -> "Date.new(2022, 10, 10)", "ReturnType" -> "ExternalObject"|>]Use "Expression" to return the object as a Wolfram Language expression:
ExternalEvaluate[session, ExternalOperation["Cast", now, "Expression"]]The Cast operation can also run in ExternalObject subvalues:
now[ExternalOperation["Cast", "Expression"]]The symbol Expression is a shortcut for the same:
now[Expression]Return the object as a string:
now[ExternalOperation["Cast", "String"]]The symbol String is a shortcut for the same:
now[String]Return the object as an ExternalObject:
now[ExternalOperation["Cast", "ExternalObject"]]The symbol ExternalObject is a shortcut for the same:
now[ExternalObject]The same can be achieved by using "ReturnType" in ExternalEvaluate:
ExternalEvaluate[session, <|"Command" -> now, "ReturnType" -> "String"|>]DeleteObject[session]Applications (1)
Define a Wolfram Language function that calls a custom Ruby function:
session = StartExternalSession["Ruby"];evenTest = ExternalEvaluate[session, "def is_even(x)
if x%2 == 0
return true
else
return false
end
end
"]{#, evenTest[#]}& /@ Range[10]//TableFormDeleteObject[session]See Also
Related Guides
Related Workflows
- Configure Ruby for ExternalEvaluate
History
Introduced in 2020 (12.1)