Expression and Statements

Expressions

Statements

Method Call Expression

Method calls in Goby looks like:

[1, 2, 3].first

foo.bar

"String".split("r")

The object before . is the receiver, and the identifier after . is method name.

Receiver can be any kind of objects, including a special expression self, which returns the current scope object.

class Foo
  def self.bar
    10
  end

  puts(self.bar) #=> 10
end

But you can also avoid using self, the method will automatically consider self as receiver if no receiver is specified.

class Foo
  def self.bar
    10
  end

  puts(bar) #=> 10
end

Assign Statement

Normal Assignment

In Goby, we support normal assignment like:

  • a = 1
  • @foo = bar
  • Constant = 123

We also support chained assignment like

a = b = c = [1, 2]

And the chained assignment is just a kind of syntactic sugar, which is equivalent to

a = [1, 2]
b = [1, 2]
c = [1, 2]

So changing one of the variables won't effect others

a = b = [1, 2]
b[0] = 2
puts(b) #=> [2, 2]
puts(a) #=> [1, 2]

Multi-variable Assignment

Multiple variable assignment is also supported in Goby:

a, b = [1, 2]
puts(a) #=> 1
puts(b) #=> 2

But we only expect the assigned value to be an Array object, which means we do not support something like:

a, b = 1, 2

And if the number of array's elements are less than the number of variables, those additional variables's value would be nil:

a, b = [1]
puts(a) #=> 1
puts(b) #=> nil

Note

We do not support multi-variable assignment and chained assignment for Constant, only for local variable and instance variable

About Return Value

In Ruby, assignment will return value if you need them, like chained assignment or assign variable in if statement's condition. Goby also support this feature, so the following use cases are all available in Goby

if @user = User.find(id)
  # do something
end

puts(a = 10)

If Statement

Unlike Ruby, Goby only supports one way to write if statement:

if foo
  bar
end

bar if foo # Not allowed

Also, Goby also support both else and elsif keywords.

if false
  foo
elsif true
  bar
else
  buz
end

Just like assign statement, if statement can also used as expression, which just like Ruby. And the default return value of if statementisnil:

a = if true
  10
end

puts(a) #=> 10

b = if false
  10
end

puts(b) #=> nil

Class/Module Statement

In Goby, class and module statements is just like Ruby's:

class Foo
  def bar
    # do something
  end
end

But currently we haven't support syntactic sugar for define singleton class like

class << self
end

Include & Extend

Both includeand extend are supported in Goby, usage is just like in Ruby:

Include

module Bar
  def bar
    10
  end
end

class Foo
  include Bar
end

puts(Foo.new.bar) #=> 10

Extend

module Foo
  def ten
    10
  end
end

class Bar
  extend Foo
end

puts(Bar.ten) #=> 10

Method Definition (Def Statement)

This is a sample of method definition in Goby

def foo
  10
end

puts(foo) #=> 10

Arguments

def add(x, y)
  x + y
end

add(10, 11)  # 21
add(10)      # Argument Error
add(1, 2, 3) # Argument Error

Just like in most languages, if you requires parameters in a method, then you need to provide right number of arguments them when calling that method. Less or more would cause argument error.

Argument's default value

But in Goby, we have another way to define method parameters: give them a default value

def add(x, y = 10)
  x + y
end

add(10, 11) # 21
add(10)     # 20

However, please make sure you place the parameters with default value after those normal parameters. Otherwise, you might encounter unexpected behavior:

def add(x = 10, y)
  x + y
end

add(10, 11) # 21
add(11) # 21, because 11 is assigned to y instead of x

results matching ""

    No results matching ""