Version 7 (modified by Chuck, 15 years ago) |
---|
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 languages
- 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 by 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
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