Jump to content

Module:Repr/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Import from https://github.com/Ozzypig/repr/blob/master/README.md with blacklisted links removed
 
Cleanup and wikify
Line 1: Line 1:
<syntaxhighlight lang="lua" inline>repr</syntaxhighlight>. is a simple Lua function that works a lot like [https://docs.python.org/3/library/functions.html#repr Python's repr]. It converts a Lua value into a printable string.
# repr


== Usage ==
`repr` is a simple Lua function that works a lot like [Python's repr](https://docs.python.org/3/library/functions.html#repr). It converts a Lua value into a printable string.

## Quick Install

Paste the following into the **Command Bar** (View &lt; Command Bar) in Roblox Studio. It will use HttpService to download and insert repr into ReplicatedStorage automatically.

```lua
local Url = "https://raw.githubusercontent.com/Ozzypig/repr/master/repr.lua"
local HttpService = game:GetService("HttpService")
local httpEnabled = HttpService.HttpEnabled
HttpService.HttpEnabled = true
local script = Instance.new("ModuleScript", game:GetService("ReplicatedStorage"))
script.Name = "repr"
game:GetService("Selection"):Set{script}
script.Source = HttpService:RequestAsync{Url=Url;}.Body
HttpService.HttpEnabled = httpEnabled
```

Alternatively, [&darr; Download the latest model file from GitHub releases](https://github.com/Ozzypig/repr/releases/latest/download/repr.rbxmx)

## Usage


Call the function with any single value and it will return a string.
Call the function with any single value and it will return a string.


<syntaxhighlight lang="lua">
```lua
local repr = require("Module:Repr").repr
-- Note: On Roblox, you can require the following asset ID:
local repr = require(3148021300)


local myTable = {
local myTable = {
Line 35: Line 14:
}
}
print(repr(myTable)) --> {hello = "world", isCool = true, score = 5}
print(repr(myTable)) --> {hello = "world", isCool = true, score = 5}
</syntaxhighlight>
```


The second value can be a table of options, which allows you to **pretty-print** with newlines:
The second value can be a table of options, which allows you to **pretty-print** with newlines:


<syntaxhighlight lang="lua">
```lua
print(repr(myTable, {pretty=true}))
print(repr(myTable, {pretty=true}))
</syntaxhighlight>
```


Here is an example that shows off all the bells and whistles:
Here is an example that shows off all the bells and whistles:


<syntaxhighlight lang="lua">
```lua
local repr = require(3148021300)
local repr = require("Module:Repr").repr
local myTable = {
local myTable = {
Line 71: Line 50:
spaces = 3; -- when pretty printing, use how many spaces to indent?
spaces = 3; -- when pretty printing, use how many spaces to indent?
tabs = false; -- when pretty printing, use tabs instead of spaces?
tabs = false; -- when pretty printing, use tabs instead of spaces?
robloxFullName = false; -- when printing Roblox objects, print full name or just name?
robloxProperFullName = true; -- when printing Roblox objects, print a proper* full name?
robloxClassName = true; -- when printing Roblox objects, also print class name in parens?
}
}
print(repr(myTable, reprSettings))
print(repr(myTable, reprSettings))
</syntaxhighlight>
```

## Development and Building

You can use [Rojo](https://github.com/Roblox/rojo) 0.5.x to build repr into a `repr.rbxmx`, which you can include in your Roblox places.

```bash
$ rojo serve
$ rojo build --output repr.rbxmx
```

Building repr into a model file can also be done by invoking GNU `make`.

## Version History

* 5 May 2019 - v1.0 Initial release
* 6 May 2019 - v1.1 Now supports all Roblox data types (Vector3, Color3, etc.)
* 5 June 2019 - This repository was created.
* 28 August 2020 - v1.2. DateTime support; added Rojo [project](default.project.json) and [Makefile](Makefile) to this repository.

## License

This work (the Lua code, model, and plugin) is licensed under the WTFPL (http://www.wtfpl.net). Go nuts. Let me know if it helped you make something cool. Please don’t to use it for world domination (it’s where I keep my stuff). A copy of the WTFPL is provided in the LICENSE file.

Revision as of 16:05, 21 February 2021

repr. is a simple Lua function that works a lot like Python's repr. It converts a Lua value into a printable string.

Usage

Call the function with any single value and it will return a string.

local repr = require("Module:Repr").repr

local myTable = {
   hello = "world";
   score = 5;
   isCool = true;
}
print(repr(myTable)) --> {hello = "world", isCool = true, score = 5}

The second value can be a table of options, which allows you to **pretty-print** with newlines:

print(repr(myTable, {pretty=true}))

Here is an example that shows off all the bells and whistles:

local repr = require("Module:Repr").repr
 
local myTable = {
	hello = "repr",
	usefulness = 9001,
	isEasyToUse = true,
	array = {"numerical", "arrays", "are", "easy"},
	land = workspace["a b c"]["1 2 3"],
	subTables = {
		moreInfo = "calls itself recursively to print sub-tables"
	},
	usesToString = {__tostring = function () return "__tostring functions are called automatically" end},
	["$YMBOL$"] = "keys that aren't Lua identifiers are quoted";
	[{also = "tables as keys work too";}] = "in case you need that",
	cyclic = {note = "cyclical tables are printed as just {CYCLIC}"}
}
-- create a cycle:
myTable.cyclic.cyclic = myTable.cyclic
 
local reprSettings = {
	pretty = false;              -- print with \n and indentation?
	semicolons = false;          -- when printing tables, use semicolons (;) instead of commas (,)?
	sortKeys = true;             -- when printing dictionary tables, sort keys alphabetically?
	spaces = 3;                  -- when pretty printing, use how many spaces to indent?
	tabs = false;                -- when pretty printing, use tabs instead of spaces?
}
print(repr(myTable, reprSettings))