Can Opal classes be extended from javascript ?

54 views
Skip to first unread message

msree...@gmail.com

unread,
Jul 12, 2014, 8:29:33 AM7/12/14
to opa...@googlegroups.com
Any pointers on calling opal code from js is much appreciated.

Currently I use coffeescript and it suffers from the same problem, but
it's possible.


Elia Schito

unread,
Jul 12, 2014, 8:58:16 AM7/12/14
to opa...@googlegroups.com
It's certainly possible. If you have a specific example I'll show you the correct code. Also you can give a look at this page http://opalrb.org/docs/generated_javascript/

Elia
--
You received this message because you are subscribed to the Google Groups "opalrb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opalrb+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sreeharsha Mudivarti

unread,
Jul 12, 2014, 1:33:52 PM7/12/14
to opa...@googlegroups.com

On Sat, Jul 12, 2014 at 6:28 PM, Elia Schito <el...@schito.me> wrote:
http://opalrb.org/docs/generated_javascript/

Not explicit mention of classes.

For an example, you can have a look at -- http://coffeescript.org/#classes
To make js compatible coffeescript, I had to https://github.com/harsha-mudi/yx

I even added a rudimentary mixin support. Instead of doing all that,
I could use ruby, no ?

Elia Schito

unread,
Jul 12, 2014, 4:20:25 PM7/12/14
to opa...@googlegroups.com
Opal classes are real instances of the class Class (as in Ruby)
If you want to create an opal class from JS I think this code should work

%x[
// class construction &block
Opal.Class.$new._p = function(){
  Opal.defn(this, '$initialize', function(a,b){
    this.$puts(a,b);
  })
  
  Opal.defn(this, '$greet', function(){
    this.$puts("hello");
  })
}; A = Opal.Class.$new()

// the Ruby way
A.$new(1,2)

// using the constructor
a = new A._alloc
a.$initialize(3,4)
]

A = `A`
A.new 5,6

Hope this is what you were looking for :)

Sreeharsha Mudivarti

unread,
Jul 12, 2014, 4:55:42 PM7/12/14
to opa...@googlegroups.com
Fascinating.

An example of extends, mixins, pretty please :)


--
You received this message because you are subscribed to a topic in the Google Groups "opalrb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/opalrb/CNXTPb_gPvA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to opalrb+un...@googlegroups.com.

Elia Schito

unread,
Jul 12, 2014, 5:09:31 PM7/12/14
to opa...@googlegroups.com
Consider that include and extend are just methods, all the semantics are just part of their implementation.

To create a new module replace Class with Module in the previous code and you're ready to go.

Then supposing you created a module named B

A.$include(B);
Opal.Object.$new().$extend(B);

should both work correctly

Sreeharsha Mudivarti

unread,
Jul 12, 2014, 7:12:26 PM7/12/14
to opa...@googlegroups.com
Thx !

I'll look into this further.

btw there are some broken links in http://opalrb.org/docs/compiler/

Elia Schito

unread,
Jul 12, 2014, 8:56:05 PM7/12/14
to opa...@googlegroups.com
I'll fix the links, thanks for reporting, let me know if you find anything else we can fix or improve. 

Elia
Reply all
Reply to author
Forward
0 new messages