tutorial// Ruby to Euphoria
- Posted by _tom (admin) May 24, 2016
- 1911 views
Warning:
I edited this entry to respect the comments that follow:
- no intent to insult Ruby
- comments are incomplete because I do not program in Ruby
- notes are made as a starting point for my understanding of Ruby, nothing else
_tom
Going from Ruby to Euphoria involves learning to think simpler.
Ruby reference: The Book of Ruby H. Collingbourne ( www.sapphiresteel.com )
"Ruby’s syntax may look simple at first sight but, the more you get to know the language the more you will realize that it is, on the contrary, extremely complex."
Some observations on the differences between Ruby and OpenEuphoria.
Ruby | Euphoria |
very entity oriented (OOP) | not based on entities |
They say "object" I say "entity" because we use object as a data-type.
OOP is trendy and popular with many programmers. All OOP languages have the alias problem where a copy does not produce a new unique object. This requires you learn how to clone objects.
Euphoria does not have dot notation.
OOP languages emphasize polymorphism as a way to make code gerenic and versatile. Euphoria is naturally generic so you will find functions that work on strings can work on raw numbers just as nicely.
Extra code to create and then use entities may complicate small programs.
Ruby | Euphoria |
self-modifiying code possible | no self-modifiying code |
First, Euphoria is interpreted and apps can be distributed as a "bound" executable. Second, Euphoria can be translated and compiled using C compiler. This close compatibility with "C" means that code is not self-modifying.
Ruby | Euphoria |
no declarations, inferred types | explicit declarations; only three primary data-types |
Euphoria has a "no surprise" police; that means no variables become declared or have values assigned in some spontaneous fashion without your explicit intentions. The Ruby NIL value does not magically appear when you forget to assign a value to a variable.
In a array if you output an non existing item you get NIL. In Euphoria you get an error message that the non-existing item does not exist.
That means all variables must be declared. We use atom ( individual value) and sequence (list of values) to show the intended use of a variable. But, you could use object which is the universal data-type.
Note that real, integer, character data-types are all atom.
I feel this "no surprise" policy makes reading Euphoria code easier.
Ruby | Euphoria |
creative syntax | simple pedictable syntax |
In Ruby you can, sometimes, leave out parentheses as part of a routine name: print and print() can be the same. In Ruby you can leave out the "then" keyword in a if statement (except when you can't leave it out.) You have one syntax in Euphoria with no shortcuts.
A multiline comment must have start and end markers flush with the left margin. Ruby syntax may follow different rules depending on context. Euphoria is always consistent.
Ruby uses just the "end" keyword to terminate a code block. Euphoria always include the starting keyword name: an "if" statement terminates with "end if". In the case of nested code this makes Euphoria easier to read.
Euphoria syntax is fixed but freeform. You can not leave out keywords, but you can choose the layout , spacing, and line breaks when you write code to suite you personal style.
Ruby | Euphoria |
use global variables a lot | global variables are rare; better to select from export or plublic |
fine grained control of scope |
See documentation on scope to understand the Euphoria way.
Ruby | Euphoria |
three styles "Hello" << " world" "Hello" + " world" "Hello" " world" |
one style "Hello" & " world" |
Ruby concatentation is tricky. Euphoria concatenation is the same regardless of the items.|
Ruby | Euphoria |
constant has first letter capitalized | use constant keyword, use any valid identifier |
Ruby has some specific rules for identifiers.
Ruby | Euphoria |
special trailing ! added to routines to work around alias problem | no alias problem, all routines work the same |
A Ruby routine named capitalize! (with !) will modify a string. Routines without the (!) result in a new entity.
Ruby | Euphoria |
for output need .to_s (to string) methods | for string output use puts, for number output use ? or print |
Ruby | Euphoria |
zero based indexing | one based indexing |
s[1,3] | s[2..4] |
s[-1] | s[$] |
Indexing logic in Ruby is different.
given = "Hello" then s[1] is e and s[1,3] is ell (second value is for number of items)
Euphoria
given s = "Hello" then s[2] is e and s[2..4] is ell
All Euphoria index values are postive. Ruby uses negative indexing to count backwards from the end. In Euphoria the $ marks the last itme in a sequence list so that $-1 is the second last item.
Ruby | Euphoria |
def keyword for function defintions | keyword procedure for if no value returned keyword function if value must be returned |
Intent of a routine is explicit and clear form the procedure and function keywords. A function must return a value, however these return values may be ignored.
Ruby | Euphoria |
creative | could use while ... end while loop for almost everything |
From my reference, p75, you can write code that looks like
def tired # some code ... end def snore # some code ... end def sleep # some code ... end while tired do sleep end while tired sleep end sleep while tired begin sleep snore end while tired
If you think in Ruby this all makes sense.
Euphoria is a lot simpler than Ruby, much less to learn.
_tom