core : object

Object is not only the base object for every other object in Brat, it is also the top-level object. These two ideas put together mean you can use any of object's methods anywhere.
||   !=   &&   ==   add_method   apply   array?   ask   call_method   del_method   exception?   exit   export   false?   function?   g   get_method   has_method?   hash?   import   include   includes   invoke   load_path   local_methods   loop   methods   my   not   null?   number?   object?   p   parent   print   program_args   protect   prototype   random   regex?   sleep   squish   string?   tap   throw   to_s   true?   until   when   while   with_this  

(object) ||

lhs   rhs

Performs boolean “or”. The value on the right-hand side should generally be a function to provide short-circuiting.

(boolean) !=

object1 != object2

Compares two objects, then negates the result.

(object) &&

lhs && rhs

Performs boolean “and”. The value on the right-hand side should generally be a function to provide short-circuiting.

(boolean) ==

object1 == object2

Compare two objects. If the target of the call has a method called <=> that will be used to compare the objects.

(self) add_method

object.add_method name, function

Add a new method to the object with the given name.

(object) apply

apply method, arg_array

Calls the provided method with the arguments in the array.

 mult = { x, y | x * y }
 apply ->mult [2, 4]      # returns 8

(boolean) array?

object.array?

Returns true if object is an array, false otherwise.

(string) ask

ask prompt

Prints out the given prompt first, then returns input from standard input.

(object) call_method

object.call_method name, arguments

Calls the given method on the object, passing in the provided arguments.

 object.call_method "p", "hello", " ", "world"

(self) del_method

object.del_method name

Removes the method with the given name from the object.

(boolean) exception?

object.exception?

Returns true if object is an exception, false otherwise.

(object) exit

exit
exit code

Immediately terminates the program. If a numeric code is provided, that will be the exit status of the program.

(object) export

export object, name

Exports the object to be imported into another file using the given name.

(object) false?

object.false? object.false?
false? condition
false? condition, then_value
false? condition, then_value, else_value

Tests if an object or condition is false. If the condition is false, returns true or the then_value if one is given. If the condition is true, returns false or the else_value if one is given. Typically, then_value and else_value should be functions, to allow for delayed evaluation.

   false? 2 + 2 == 5
     { p "Definitely false." }
     { p "War is Peace" }

(boolean) function?

function? variable

Returns true if given variable is a function, false otherwise.

(string) g

g

Read a string from standard input.

(function) get_method

object.get_method name

Returns the method with the given name, or null if it does not exist.

(boolean) has_method?

object.has_method? name

Returns true if the object has a method with the given name.

(boolean) hash?

object.hash?

Returns true if object is a hash, false otherwise.

(object) import

import file
import file, object

Loads the given file and returns a hash of the exports from that file. If an object name is specified, only that object will be returned.

(object) include

include file
include file, object

Executes given file and adds any exported objects to the current context. If an object is specified, it will only import that object. This is essentially equivalant to calling squish(import(file)).

 include :file

(object) includes

includes files

Calls include for each of the given arguments.

 includes :file :json

(object) invoke

invoke method, *args

Calls the provided method with the arguments in the array.

 mult = { x, y | x * y }
 invoke ->mult 2 4        # returns 8

(array) load_path

load_path

The path used to search for files to load when using include() or import().

(array) local_methods

object.local_methods

Returns an array containing the names of the methods available on the object, not including those inherited from parent objects.

(object) loop

loop block

Loops block forever.

(array) methods

object.methods

Returns an array containing the names of the methods available on the object, including those inherited from parent objects.

(self) my

my

Returns the current object.

(boolean) not

not value

Returns true if value is false, false if value is true.

(object) null?

object.null?
null? condition
null? condition, then_value
null? condition, then_value, else_value

Tests if an object or condition is null. If the condition is null, returns true or the then_value if one is given. If the condition is not null, returns false or the else_value if one is given. Typically, then_value and else_value should be functions, to allow for delayed evaluation.

   null? x
     { p "x is null" }
     { p "x is not null" }

(boolean) number?

object.number?

Returns true if object is a number, false otherwise.

(boolean) object?

object? variable

Returns true if given variable is an object, false otherwise.

(null) p

p *args

Prints out any number of arguments, with an added new line.

(object) parent

object.parent

Return parent object if it exists.

(object) parent

object.parent

Returns the parent of the object.

(null) print

print *args

Prints out any number of arguments, with no new line.

(array) program_args

program_args

Returns the arguments given to the program when it is executed.

(object) protect

protect block, options

Handles exceptions which may be thrown inside the block. Options should be provided as a hash. If no options are given, all exceptions will be silently ignored. Possible options: * rescue: provide a function to call when an exception occurs * ensure: provide a function to always call, even if an exception occurs * from: only rescue a specific type of exception

 protect { throw "Problem!" } rescue: { err | p "There was a problem: #{err}" }

(prototype) prototype

object.prototype
object.prototype block
object.prototype method_hash

Set functions for the prototype of the object. The prototype is used when constructing a child of the object (via new). This function can either take a block which will execute in context of the prototype, or a hash table of method names and the methods themselves. Alternatively, calling object.prototype returns the prototype object, so methods can be defined directly on that object.

 person = object.new

 person.init = { name |
   my.name = name
   my.status = "nuttin'"
 }

 person.prototype walk: { my.status = "walking" }

 person.prototype {
   my.talk = { phrase | p "#{my.name} says, '#{phrase}'" }
 }

 person.prototype.sit = {
   my.status = "sitting"
 }

(number) random

random random maximum

With no arguments, returns a number between 0 and 1. With a max argument, returns a number i, where 0 <= i < max.

(boolean) regex?

object.regex?

Returns true if object is a number, false otherwise.

(number) sleep

sleep seconds

Sleep for a given number of seconds.

(self) squish

object.squish other_object

Squishes the methods of the given object into the current object. Also useful for bringing external libraries into the current context.

(boolean) string?

object.string?

Returns true if object is a string, false otherwise.

(self) tap

object.tap block

Calls given block in context of the object, passing in the object as an argument, and always returns the object.

(object) throw

throw exception

Throws an exception. If a string is provided as the exception, creates a new exception with the string as the error message.

(string) to_s

object.to_s

Return a string representing the object.

(object) true?

object.true?
true? condition
true? condition, then_value
true? condition, then_value, else_value

Tests if an object or condition is true. If the condition is true, returns true or the then_value if one is given. If the condition is false, returns false or the else_value if one is given. Typically, then_value and else_value should be functions, to allow for delayed evaluation.

   true? 2 + 2 == 5
     { p "Are you such a dreamer?" }
     { p "No it doesn't!" }

(object) until

until block
until condition, block

Loops until the condition becomes true. If only one argument is given, that argument will be used as the condition.

  x = 0
  until {
    x = x + 1
    x > 10
  }

  #Same thing
  x = 0
  until { x > 10 } { x = x + 1 }

(object) when

when condition, result

Takes any number of condition-result pairs. Checks each condition and when one returns true, returns the result associated with that condition.

 x = 3
 when { x < 3 } { p "x is less than 3!" }
  { x > 3 } { p "x is greater than 3!" }
  { x == 3 } { p "x is exactly 3!" }

(object) while

while block
while condition, block

Loops while the given condition remains true. If only one argument is given, the value of that argument is used as the condition.

  x = 0
  while {
    x = x + 1
    x < 10
  }

  #Same thing
  x = 0
  while { x < 10 } { x = x + 1 }

(object) with_this

object.with_this block, arguments

Calls the given method, but sets the target object as the scope. In other words, calling my inside the block will return the object.

 x = object.new
 x.greeting = "Hello!"

 #Will print "Hello!"
 x.with_this { p greeting }
Fork me on GitHub