== 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 [http://cobra-language.com/trac/cobra/browser/cobra/trunk/Tests/320-misc-two/820-anonymous-methods-aka-closures test cases] for examples. Also of interest to Ruby users, Cobra has [MixIn mix-ins] and [TypeExtensions 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 [PythonLanguage 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 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 == === Command line arguments === Ruby: {{{ #!ruby ARGV.each {|arg| puts arg} }}} Cobra: {{{ #!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: {{{ #!cobra class A def main for arg in CobraCore.commandLineArgs[1:], print arg }}} === Class Declaration === Ruby: {{{ #!ruby class A < B def foo puts 'foo' bar end def bar print 'bar' end end }}} Cobra: {{{ #!cobra class A inherits B def foo print 'foo' .bar def bar print 'bar' stop }}} === Instantiation === Ruby: {{{ #!ruby obj = SomeClass.new(arg1, arg2) }}} Cobra: {{{ #!cobra obj = SomeClass(arg1, arg2) }}} === Blocks === Ruby: {{{ #!ruby method do |arg| end }}} Cobra: {{{ #!cobra .method(do(arg)) }}} === Conditionals === Ruby: {{{ #!ruby if x < y then puts x end puts x if x < y }}} Cobra: {{{ #!cobra if x < y, print x if x < y, print x }}} Ruby: {{{ #!ruby if x.nil? puts 'x is nil' end }}} Cobra: {{{ #!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 == * OtherLanguages * LanguageTopics * HowToPrograms * PrimitiveTypeMembers