-
Notifications
You must be signed in to change notification settings - Fork 10
Syntax
Comments in Blueberry can be one liners and multiline
# this is a single line comment
/* this
is a multiline
comment */
number = 1
number = 1.2 # floating point number
str = "my string"
sym = :my_symbol # it's basically just an string but can only have [a-zA-Z_] characters
nothing = nil # equivalent to PHP's null
obj = new MyClass() # instantiate an object
You can assign values to variables just by using =.
my_variable_name = "my value"
if age > 18 and age < 99
doSomething()
else if age > 99
doSomethingElse()
else
doNothing()
end
You can also use switch
switch someVariable
when "mike"
echo("Hello, Mike!")
when "john"
echo("Hello, John!")
when "annie", "marceline"
echo("Hai gals")
else
echo("Hello, World!")
end
To apply a boolean not operation, use the not keyword
if not age > 18
doSomething()
end
myValue = not myVariable
title = age > 18 ? "Mister" : "Boy"
name = getName() ?? "Mike"
If getName() is falsy, "Mike" will be used, that translates as
name = getName() ? getName () : "Mike"
Blueberry has several loop flavours, while and for beeing the most
common ones.
while someFunctionReturnsTrue()
echo("Hello, World!")
end
# The for is very similar to ruby's :)
for i in (0..10)
echo(i)
end
Ranges generate an array from start to end, so you can also do
myArr = (0..returnsInteger())
You can of course call for with any collection
myData = array(1, 2, 3, 4)
for i in myData
echo(i)
end
The concatenation operator is &.
echo("Hello " & name)
You can create arrays with []
Also, arrays can have initial values
myArray = [1, 2, f(1)]
callSomething([1, 2])
emptyArray = []
To get the array item at a given index is the same as PHP
item = arr[2]
Arrays indices start from 0
You can create multidimensional arrays using JSON syntax
myArray = {
"name": "John",
"age": getAge(),
"more_data": {
"married": false
}
}
try
someCodeHere()
catch myError
echo(myError.getMessage())
finally
doSomethingElse()
end
Of course you can omit the finally and the catch argument
try
someCodeHere()
catch
# When you don't include an argument identifier, "ex" is used
# Nevertheless, most of the time you don"t include one it"s because you wont
# use it.
echo(ex.getMessage())
end
Functions are created using the def keyword
def myFunctionName(name)
return "Hello" & name
end
Classes include the @ operator from Ruby, using that operator you can
create instance variables and later on refer to them in a very simple and
readable way.
class Human
# name will be a public instance variable
@name
# age will also be a public instance variable with a value of 18
@age = 18
def __construct(name)
# this is the class's constructor
@name = name
end
def greet
echo("Hi! I'm " & @name)
end
end
mike = new Human()
mike.greet()
echo("Mike is " & mike.age & " years old.")
You can also use access modifiers
class MyClass
private @name
protected def getName()
return @name
end
end
If no access modifier is used, public is used.
Inheritance is done Ruby-Style
class B < A
# Class B extends from A
end
In PHP you can define that a variable is a reference to another by using &, in Blueberry, it's the same
a &= b # a is a reference to b
def myFunc(&a)
# a is passed by reference and the value will be modified outside the function
a = a + 1
end
num = 1
myFunc(num)
# num is now 2