These are chat archives for opal/opal

11th
Jun 2015
Forrest Chang
@fkchang
Jun 11 2015 00:19
@Hospira-MedNet I think http://opalrb.org/docs/compiled_ruby/#ruby-from-javascript will tell you what u need
Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 05:54
can any answer share your thoughts on this
Forrest Chang
@fkchang
Jun 11 2015 06:05
@Hospira-MedNet not sure what you don't understand, if you compile the Foo class, then the way you would access it is Opal.Foo.$new().$bar();
Because classes get hung of the Opal object in JS, thus the Opal.Foo, and opal methods get prefixed with $, so to create a Foo object, you'd do Opal.Foo.$new(), and you would just call $bar() off of that
Opal.Foo.$new().$bar(); is basically what Foo.new.bar would get compiled to
Actually, what gets compiled is return $scope.get('Foo').$new().$bar(); after all the opal setup, but if you wanted the equivalent of Foo.new.bar in javascript, you'd write Opal.Foo.$new().$bar();
Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 06:51
@fkchang , So first we need to compile the ruby code in .rb file , so i called like Opal.compile(Foo). But i am getting some exceptions
Forrest Chang
@fkchang
Jun 11 2015 07:02
@adambeynon @meh @elia @vais shouldn't an instance Native::Object return something other than nil for methods? Seems more consistent behavior
Elia Schito
@elia
Jun 11 2015 08:02
@fkchang can you provide an example? (just to help me understand)
Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 08:17
@fkchang let me know whether am i compiling correctly. If not , Please let me know on how to compile class Foo
Vais Salikhov
@vais
Jun 11 2015 12:11

@adambeynon @elia guys, with arity check enabled (which is how specs are running), the following fails with an ArgumentError, wrong number of arguments(1 for 0)

def test
  puts 'ok'
end
a = nil
test(*a)

But this works on MRI just fine. I think splat with a nil is a special case. Any idea where I could begin fixing this? I need this to work to pass some new rubyspecs...

Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 12:25
can anyone give me some suggestions on my question plzz
Vais Salikhov
@vais
Jun 11 2015 12:31
@Hospira-MedNet in your stackoverflow question it looks like you are answering your own question. It's not clear what exactly is your question??
Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 12:42
@vais my question is how i can call ruby method in my protractor tests (.JS file)
@fkchang told me that you need to compile first.. so i called like Opal.compile(Foo) in my ruby file but i got some exceptions
Vais Salikhov
@vais
Jun 11 2015 12:51
@Hospira-MedNet the easiest way for you to compile file.rb into file.js is to write this on the command line:
opal -c file.rb > file.js
Now you can include file.js into your html page using a script tag, like any other javascript file. Make sense?
Srikanth Kuppuswamy
@Hospira-MedNet
Jun 11 2015 12:59
i did i am getting other general opal related stuff with at the end our class name is now converted into function name
as you said using script tag we can call this .js file
so i can call directly like this Opal.Foo.$new().$bar(); in .JS file am i right?
Vais Salikhov
@vais
Jun 11 2015 13:16
@Hospira-MedNet yep, what you get in that .js file is all of Opal compiled to javascript + your ruby compiled to javascript. Now if you include this file into your page, other javascript included after this file should be able to call into your ruby-compiled-to-js code. Hope this helps!
CJ Lazell
@cj
Jun 11 2015 14:34
@vais found another bug to do with procs
opal/opal#932
I created an opalrb gist with a refined example
Vais Salikhov
@vais
Jun 11 2015 15:30
@cj wait, that's the refined example?! Oh...
Elia Schito
@elia
Jun 11 2015 15:30
lol
Forrest Chang
@fkchang
Jun 11 2015 16:20
@elia file:///Users/fkchang/src/opal-irb/index-embeddable.html#code:w%20%3D%20Native(%60window%60)%0Aw.methods
sorry, lemme give you the github.io link
If I were to guess what's happening, Native::Object inherits from BasicObjects and since it may not support methods(), it's instead call the methods property on @navtive, which is generally null
Elia Schito
@elia
Jun 11 2015 16:22
@fkchang Native(`{methods: 123}`)
that's supported on the other hand
Forrest Chang
@fkchang
Jun 11 2015 16:23
The reason I think it's inconsistent is that you expect ruby objects to know what their methods are
Elia Schito
@elia
Jun 11 2015 16:23
not sure what's the right thing to do
Forrest Chang
@fkchang
Jun 11 2015 16:24
it's a pain to have to do an if obj.is_a?(Native::Object) blah and if native?(obj)
kind of defeats the everything is an object beauty of Ruby

@elia on a different front when would one want to do

class Wrapper < `JsClass`
  alias_native :method_name
  def initialize
    `return new JsClass()`
  end
end

vs

class Wrapper
  include Native
  def initialize
     @native = `new JsClass()`
   end
end
Forrest Chang
@fkchang
Jun 11 2015 16:31
@Hospira-MedNet can you make a gist so we can see what your doing?
Mitch VanDuyn
@catmando
Jun 11 2015 18:18
Folks... does anybody have some concepts for internationalization? We are hitting a bunch of code that depends on internationalization, and are just wondering if anybody has come up with a general solution for getting this stuff to the browser for opal code to use...
Michał Kalbarczyk
@fazibear
Jun 11 2015 22:03
Hi, I’m trying to make opal-slim work but get NoMethodError: undefined methodindent_dynamic' for Temple::Utils`. Every one use it ? Thanks
Michał Kalbarczyk
@fazibear
Jun 11 2015 22:08
@jgaskins, any clues ?
Mitch VanDuyn
@catmando
Jun 11 2015 22:15
I am trying to make a directory in rails (lets call it app/shared) that will contain ruby code that rails will find on the server (I think this is the easy part) but also that will get compiled and downloaded with the assets. Any clues? (anybody else?)
Michał Kalbarczyk
@fazibear
Jun 11 2015 22:17
@catmando maybe that will help
# config/initializers/opal.rb
Rails.application.config.assets.paths << Rails.root.join('app', ’shared').to_s
Mitch VanDuyn
@catmando
Jun 11 2015 22:25
@fazibear - that was it! works great. thanks
Mitch VanDuyn
@catmando
Jun 11 2015 22:33
but now the problem is (not a big problem) I want to just pull in all the files in that shared directory without requiring them. I would like to keep the require_tree directive in the assets folder, but I don't think there is a way. I guess will have to add a file in the shared directory called shared.rb that just has a require_tree "." or something?
Forrest Chang
@fkchang
Jun 11 2015 22:46
@catmando don't have time to write it now, but I've been meaning to blog about how we approached the shared code in our production app
Mitch VanDuyn
@catmando
Jun 11 2015 22:47
@fkchang - any quick hints? We are right there and I don't want to reinvent the wheel.
Forrest Chang
@fkchang
Jun 11 2015 22:49
there were some issues, we had to resolve, but in short, we made a lib/client_and_server directory that we softlinked into app/assets/javascripts
we did have some load order issues that we had to specifically specify require order, which may or may not hit you
Mitch VanDuyn
@catmando
Jun 11 2015 22:50

So far I am setting up a directory shared, which will contain ruby code that will get included in the server as well as the client... So for works okay, except I wanted to do this:


class Jobs
    # bunch of stuff that only is relevant to the server
end

and in the shared directory


class Jobs
   # stuff related to both client and server
end

The only problem is that rails will only find one (I think) so one has to require the other... not a big deal I guess

why did you go with the softlink approach and not just a directory at the top level (i.e. app/client_and_server)
Forrest Chang
@fkchang
Jun 11 2015 22:52
I would put the common code in the lib/client_and_server dir, and the extend the class respectively in the appropriate server/link
re softlink, I guess we could've done that. We messed around w/autoloader no autoloading on both front and back end for various things, ended up with something different than we started with
could've worked w/the toplevel dir
I'd be curious to what @elia and @adambeynon have done, they've deployed the most opal rails apps of anyone
Mitch VanDuyn
@catmando
Jun 11 2015 22:57
@fkchang - yeah I guess the extend will be okay, but it would be nice to just have it be automagic
Forrest Chang
@fkchang
Jun 11 2015 23:13
u could check the ruby engine for opal in a single file
not certain I like the cleanliness of that, but it'd work w/o extending the classes