Foldr Zen Zone

MaSH

Objects

Objects in MaSH are used to store related data. Data is stored in key/value pairs and… hang on, this sounds an awful lot like the dictionaries that we met in a previous chapter. That’s because a dictionary in MaSH is a type of object. Objects can store two types of data in their key/value pairs, properties and methods. Properties are exactly what we saw with dictionaries, the key name is used to store a value which can be one of our basic types (string, number, array and even another dictionary). Properties can be accessed in the same manner which we saw when accessing values inside a dictionary (this is known as array access) but also using something known as dot notation.

Natural

mash
set myDictionary to { name: "Grace Hopper", occupation: "Computer Scientist" }

# Accessing a value from a dictionary using array access

printline myDictionary["name"]

# Accessing a value from a dictionary object using dot notation

printline myDictionary.occupation

Standard

mash
myDictionary = { name: "Grace Hopper", occupation: "Computer Scientist" }

# Accessing a value from a dictionary using array access

printline(myDictionary["name"])

# Accessing a value from a dictionary object using dot notation

printline(myDictionary.occupation)

Output

output
Grace Hopper
Computer Scientist

Nested Objects

As well as our simple data types, an object property can also contain another object. This is known as nesting. MaSH contains a mash object which is accessible in any mashlet. This root object contains many other objects which make up the MaSH libraries and allow us to work with files, databases, web apis, dates and many others.

Methods

So far the only type of object we have seen in MaSH is a dictionary which only contains properties. But objects can also contain methods. Like blocks methods can accept parameters and contain reusable code and can be called when required. MaSH features many built-in objects which all contain methods for performing specific tasks. To call a method you must follow its name with an open and close parentheses (). If the method takes any parameters then these are specified inside the parentheses as a list separated with commas , . The following example uses the MaSH date object and calls its now method to obtain the current date and time.

Natural

mash
# Access the date object inside the root mash object and call its now method

set myDate to mash.date.now()

printline "Currently it is {{ myDate }}"

Standard

mash
# Access the date object inside the root mash object and call its now method

myDate = mash.date.now()

printline("Currently it is {{ myDate }}")

Output

output
Currently it is 2023-08-25 20:10:29
Every journey begins with a single step

There can be many paths to a desired document. Let Foldr be your guide, wherever the destination...

Find File Zen