Coder Social home page Coder Social logo

opal / opal.github.io Goto Github PK

View Code? Open in Web Editor NEW
10.0 5.0 32.0 24.97 MB

Source for Opal website, **see the "source" branch for contributing**

Home Page: https://github.com/opal/opal.github.io/tree/source#readme

HTML 0.66% Ruby 2.99% Shell 0.02% JavaScript 50.71% SCSS 45.01% Haml 0.61%
opal opal-website

opal.github.io's Introduction


Opal
Opal-Ruby 💛 JavaScript

Opal is a Ruby to JavaScript source-to-source compiler.
It also has an implementation of the Ruby corelib and stdlib.

Community:
Stack Overflow Backers on Open Collective Sponsors on Open Collective Slack Documentation
Code:
Gem Version Build Status Code Climate Coverage Status
Sponsors:
Nebulab: Open Source Fridays

Usage

See the website for more detailed instructions and guides for Rails, jQuery, Sinatra, rack, CDN, etc. https://opalrb.com.

Compiling Ruby code with the CLI (Command Line Interface)

Contents of app.rb:

puts 'Hello world!'

Then from the terminal

$ opal --compile app.rb > app.js # The Opal runtime is included by default
                                 # but can be skipped with the --no-opal flag

The resulting JavaScript file can be used normally from an HTML page:

<script src="app.js"></script>

Be sure to set the page encoding to UTF-8 inside your <head> tag as follows:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="app.js"></script></head>
  <body></body>
</html>

Just open this page in a browser and check the JavaScript console.

Compiling Ruby code from Ruby

Opal.compile is a simple interface to just compile a string of Ruby into a string of JavaScript code.

Opal.compile("puts 'wow'")  # => "(function() { ... self.$puts("wow"); ... })()"

Running this by itself is not enough; you need the opal runtime/corelib.

Using Opal::Builder

Opal::Builder can be used to build the runtime/corelib into a string.

Opal::Builder.build('opal') #=> "(function() { ... })()"

or to build an entire app including dependencies declared with require:

builder = Opal::Builder.new
builder.build_str('require "opal"; puts "wow"', '(inline)')
File.binwrite 'app.js', builder.to_s # must use binary mode for writing

Compiling Ruby code from HTML (or using it as you would with inline JavaScript)

opal-parser allows you to eval Ruby code directly from your HTML (and from Opal) files without needing any other building process.

So you can create a file like the one below, and start writing ruby for your web applications.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.opalrb.com/opal/current/opal.js"></script>
    <script src="https://cdn.opalrb.com/opal/current/opal-parser.js" onload="Opal.load('opal-parser')"></script>

    <script type="text/ruby">
      puts "hi"
    </script>

  </head>
  <body>
  </body>
</html>

Just open this page and check the JavaScript console.

NOTE: Although this is possible, this is not really recommended for production and should only be used as a quick way to get your hands on opal.

Running tests

Setup the project:

$ bin/setup

The test suite can be run using:

$ bundle exec rake

This command will run all RSpec and MSpec examples in sequence.

MSpec

MSpec tests can be run with:

$ rake mspec

Alternatively, you can just load up a rack instance using rackup, and visit http://localhost:9292/ in any web browser.

RSpec

RSpec tests can be run with:

$ rake rspec

Automated runs

A Guardfile with decent mappings between specs and lib/corelib/stdlib files is in place. Run bundle exec guard -i to start guard.

Code Overview

What code is supposed to run where?

  • lib/ code runs inside your Ruby env. It compiles Ruby to JavaScript.
  • opal/ is the runtime+corelib for our implementation (runs in browser).
  • stdlib/ is our implementation of Ruby's stdlib. It is optional (runs in browser).

lib/

The lib directory holds the Opal parser/compiler used to compile Ruby into JavaScript. It is also built ready for the browser into opal-parser.js to allow compilation in any JavaScript environment.

opal/

This directory holds the Opal runtime and corelib implemented in Ruby and JavaScript.

stdlib/

Holds the stdlib currently supported by Opal. This includes Observable, StringScanner, Date, etc.

Browser support

  • Internet Explorer 11
  • Firefox (Current - 1) or Current
  • Chrome (Current - 1) or Current
  • Safari (Current - 1) or Current
  • Opera (Current - 1) or Current

Any problems encountered using the browsers listed above should be reported as bugs.

(Current - 1) or Current denotes that we support the current stable version of the browser and the version that preceded it. For example, if the current version of a browser is 24.x, we support the 24.x and 23.x versions.

12.1x or (Current - 1) or Current denotes that we support Opera 12.1x as well as the last 2 versions of Opera. For example, if the current Opera version is 20.x, then we support Opera 12.1x, 19.x and 20.x but not Opera 15.x through 18.x.

Contributors

This project exists thanks to all the people who contribute. contributors

Versioning

Opal will broadly follow semver as a version policy, trying to bump the major version when introducing breaking changes. Being a language implementation we're also aware that there's a fine line between what can be considered breaking and what is expected to be "safe" or just "additive". Moving forward we'll attempt to better clarify what interfaces are meant to be public and what should be considered private.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Become a Backer Button

Sponsors

Donations

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Become a Sponsor Button

Sponsored Contributions

Nebulab Logo

License

(The MIT License)

Copyright (C) 2013-2021 by Adam Beynon and the Opal contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

opal.github.io's People

Contributors

adambeynon avatar aekobear avatar bbatsov avatar benjaminoakes avatar bwl21 avatar catmando avatar codingitwrong avatar dependabot[bot] avatar elia avatar hakusaro avatar hmdne avatar jaredcwhite avatar jeremyevans avatar jessechavez avatar josephschito avatar marten avatar resynth1943 avatar suruja avatar tansaku avatar vais avatar vzvu3k6k avatar webdev778 avatar wied03 avatar wilsonsilva avatar ybiquitous avatar yhara avatar zw963 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

opal.github.io's Issues

Update "Try Opal now!", please

It seems "Try Opal now!" needs an update.

The following fails:

puts Native(`window`).document.getElementsByClassName("foo").length

Thank you.

can't define own class of User inheriting from other class

Not sure if this is worth fixing, but you can't create your own version of User and inherit from another class in the try opal section due to the user class already being defined. For example:

class User < CustomClass
end

will fail with a TypeError: superclass mismatch for class User error. Not sure how trivial it would be to reset all global state set in the editor.

Use do end in config.ru not work.

From a very simple example, expect to start a rack server use rackup.

├── Gemfile
├── Gemfile.lock
├── app
│   └── application.rb
├── config.ru
└── index.html

Following code is worked

require 'bundler'
Bundler.require

run Opal::Server.new {|s|
  s.append_path 'app'
  s.main = 'application'
end
}

But, if change to following, not worked

require 'bundler'
Bundler.require

run Opal::Server.new do |s|
  s.append_path 'app'
  s.main = 'application'
end
RuntimeError: Main asset path not configured (set 'main' within Opal::Server.new block)
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/opal-0.10.5/lib/opal/sprockets/server.rb:104:in `html'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/opal-0.10.5/lib/opal/sprockets/server.rb:92:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/deflater.rb:34:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/opal-0.10.5/lib/opal/sprockets/server.rb:79:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/tempfile_reaper.rb:15:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/lint.rb:49:in `_call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/lint.rb:37:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/common_logger.rb:33:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/chunked.rb:54:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/content_length.rb:15:in `call'
	/home/zw963/.rvm/gems/ruby-2.3.3@一个最简单的opal例子/gems/rack-2.0.3/lib/rack/handler/webrick.rb:86:in `service'
	/home/zw963/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
	/home/zw963/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
	/home/zw963/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

Getting started considered harmful

Don't get me wrong, Opal is great. Just that guide may put more people off than get them interested.
It looks like before sprockets and even earlier days, as it talks about compiling [1,2,3] and not at all about what you can really do with opal.

As a newbie i can say what i'd expect (don't want to write it, but also not just nag)

Start with the basic choice, how to use it

  • checking out -> opal server
  • medium project -> sinatra server
  • rails -> rails integration
  • client/server -> volt
  • standalone client project including js's and go

And then explain basic steps for each. And links, not just links i mean.

Follow up with some roadmap through the docs,
mention some differences ruby/opal. link to the compiler page

some basic on the choices and availability of gems (opal-browser/opal-jquery) and how to integrate those.

Hash collisions

$ bundle exec opal-repl
>> a = 'A'
=> "A"
>> a.hash
=> "A"
>> a = []
=> []
>> a.hash
=> "A"

Any ideas how to improve on this? Strings in Opal currently hash to themselves, which is what makes it possible for Hash to be very fast in 99.9% of typical use cases. But... the above is not acceptable. This is an open call for suggestions, I guess. Should we include some magic non-printable characters in hash strings on all other objects? I.e. how can we make sure there is no collision between a hash of a String and a hash of another object that happens to hash to the same value as that string? Again, currently:

>> 1.hash
=> "Numeric:1"
>> "Numeric:1".hash
=> "Numeric:1"

😕 🔫

Example at "/usage" doesn't work as described

From the text in opalrb.org/usage, it appears that you can get opal to compile a ruby file in /app simply by running a gem install opal, creating the following Rakefile, and running rake build:

desc "Build our app to build.js"
task :build do
  env = Opal::Environment.new
  env.append_path "app"

  File.open("build.js", "w+") do |out|
    out << env["application"].to_s
  end
end

I just tried this and it didn't work. I get:

$ rake build
rake aborted!
uninitialized constant Opal::Environment
C:/Documents and Settings/user/My Documents/Workspace/Code Sandbox/opal_test/Rakefile:5:in `block in <top (required)>'
Tasks: TOP => build
(See full trace by running task with --trace)

So apparently there are a few steps missing in the description on that page. Could you please update it to be a bit more thorough?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.