Wiki

Ruby

Cobra, like Ruby, has a high level syntax and garbage collection to enable rapid coding. One of the first things Ruby users want to know, is if Cobra supports blocks. Yes, it does. They are sometimes referred to as "anonymous methods" and "closures" so keep your eyes open for those terms on the discussion forums.

Blocks are indicated by the keyword "do". If there are any arguments to pass they are declared in the same way as methods and signatures: with a parenthesized list of arguments. The body of the block then follows on the next line, indented one level.

See the test cases for examples.

Also of interest to Ruby users, Cobra has mix-ins and type extensions.

Cobra has some interesting characteristics for Ruby developers including a huge increase in performance, native threads, built-in contracts, built-in unit tests and more.

Although Ruby is not Python, Ruby users may glean some characteristics of Cobra by reading comments about Python, which are more detailed.

Commonalities

  • High level syntax
  • Garbage collected
  • Emphasis on OOP
  • Dynamic binding
  • Blocks
  • Productive features
  • Easy construction and use of maps (dictionaries) and arrays (lists)

Differences

Cobra has:

  • Optional static typing, which
    • Adds additional error checking
    • Improves performance
    • But may be overly restrictive in some cases
    • The choice is yours.
  • First class support for unit tests
    • Very lightweight syntax
    • Informative assert statement
    • A test is co-located with the method it tests for the benefits of
      • Easier maintenance
      • Documentation
      • Less file switching during development
  • First class support for contracts
    • Lightweight syntax
    • Contracts are inherited -- supports OOP
    • Can complement unit tests as a quality control feature
  • Programs are compiled even when run in one shot with "cobra myprog.cobra"
    • Performance can be up to hundreds of times faster than Ruby depending on the program.
  • Native threads
    • No thread simulations, interpreter lock, etc.
    • Scale up to the number of cores in your machine.

Other differences:

  • Strings
    • Ruby has mutable strings and immutable symbols
    • Cobra has immutable strings and mutable StringBuilder

Code Examples

Command line arguments

Ruby:

ARGV.each {|arg| puts arg}

Cobra:

class A
    def main
        for arg in CobraCore.commandLineArgs, print arg

Notice that in Cobra, the first argument is the name of the program being called, while in Ruby it's the first parameter. If you were to run the Ruby script like this:

ruby a.rb one two three

You would see this as the output:

one
two
three

If we were to run the Cobra class above:

cobra.exe a.cobra -- one two three

You would see the following output:

a
one
two
three

But you can use slicing to bypass the program name:

class A
    def main
        for arg in CobraCore.commandLineArgs[1:], print arg

Class Declaration

Ruby:

class A < B

  def foo
    puts 'foo'
    bar
  end
    
  def bar
    print 'bar'
  end

end

Cobra:

class A inherits B

    def foo
        print 'foo'
        .bar
    
    def bar
        print 'bar' stop

Instantiation

Ruby:

obj = SomeClass.new(arg1, arg2)

Cobra:

obj = SomeClass(arg1, arg2)

Blocks

Ruby:

method do |arg|
  <statements>
end

Cobra:

.method(do(arg))
    <statements>

Conditionals

Ruby:

if x < y then puts x end
puts x if x < y

Cobra:

if x < y, print x
if x < y, print x

Ruby:

if x.nil?
  puts 'x is nil'
end

Cobra:

if x is nil
    print 'x is nil'

Library Calls

obj.class       # Ruby
obj.typeOf      # Cobra has non-class types like struct and enum

obj.to_s        # Ruby
obj.toString    # Cobra

Time.now        # Ruby
DateTime.now    # Cobra

x.abs           # Ruby
x.abs           # Cobra has methods on numbers as well

See Also