hasclass/core-lib
{ "createdAt": "2012-06-29T05:09:06Z", "defaultBranch": "master", "description": "A port of the Ruby 1.9.3 corelib to coffeescript/javascript/node that conforms to rubyspec.org.", "fullName": "hasclass/core-lib", "homepage": "", "language": "CoffeeScript", "name": "core-lib", "pushedAt": "2020-02-29T08:44:10Z", "stargazersCount": 542, "topics": [], "updatedAt": "2025-01-02T01:12:17Z", "url": "https://github.com/hasclass/core-lib"}Deprecated and unmaintained for years.
Section titled “Deprecated and unmaintained for years.”This library is deprecated and hasn’t been maintained for years.
RubyJS Reloaded v0.8.0-beta1
Section titled “RubyJS Reloaded v0.8.0-beta1”RubyJS got a major overhaul.
Most importantly all methods have been refactored into a functional style (similar to underscore). They can be accessed via _s, _a, _h, _n, _t.
_s.capitalize("foo") // => "Foo"_a.rindex([1,2,3,2,1], 2) // => 3_n.upto(1, 5, function(i) { console.log(i) })_h.rassoc({a: 2, b:3}, 3) // => ["b", 3]_t.strftime(new Date(), "%Y-%m-%d") // => "2013-02-11"The original object-oriented classes that allow for easy and consistent method chaining now is a (not yet optional) add-on.
R("foo").ljust(10, '-').size().times().downto(7).to_a()// => R.Array {__native__: [10,9,8,7]}If that is not enough for you, you can opt-in the experimental “god_mode”. Which extends native JS classes with RubyJS methods.
R.god_mode('rb_') // all methods are prefixed with rb_5.rb_times(function (el) { console.log(el) });More documentation (in the works): http://rubyjs.org/reloaded
Or watch my talk at RubyKaigi: http://ustream.tv/recorded/33526011
RubyJS Packages
Section titled “RubyJS Packages”RubyJS Core
Section titled “RubyJS Core”Shared functionality required for RubyJS Base.
RubyJS Base
Section titled “RubyJS Base”Implementation of Ruby methods as functional style methods.
RubyJS OO
Section titled “RubyJS OO”Object-oriented wrappers for Ruby classes. Required for test suite and legacy reasons.
RubyJS
Section titled “RubyJS”RubyJS is a port of the Ruby core-lib and provides many methods for Array, String, Numerics and more. RubyJS classes are simple wrappers around native JavaScript objects.
See RubyJS Homepage for more details. It is licensed under MIT.
MIT License
Section titled “MIT License”RubyJS is licensed under the MIT License.
rubyjs-rails Gem
Section titled “rubyjs-rails Gem”There’s a Rails asset-pipeline gem for RubyJS.
Gemfile:
gem 'rubyjs-rails'In your application.js manifest:
//= require rubyNPM Module: rubyjs
Section titled “NPM Module: rubyjs”RubyJS can be installed as an npm module.
$ npm install rubyjsThen simply require rubyjs which will add the R and RubyJS to the global object.
require('rubyjs');R.puts("hello");Contribute/Develop
Section titled “Contribute/Develop”RubyJS is currently implemented in CoffeeScript 1.6.3. It’s on the roadmap to move away from CoffeeScript to plain JS.
- Clone repository
- Run the coffee console loading the rubyjs files:
$ node> require('./ruby')> _s.swapcase("Hello world")'hELLO WORLD'- Set up development environment:
$ bundle install$ cake build # compiles and copies ruby.js it to spec/javascripts/$ cake build_tests # compiles all test files$ bundle exec guard # automatically compile coffeescript$ bundle exec rake jasmine # starts jasmine server$ open http://localhost:8888 # prayIf you get the error: pipe(): Too many open files, see following page:
https://github.com/jashkenas/coffee-script/issues/1537
For Mac OS X users the following comment helps:
https://github.com/joyent/node/issues/2479#issuecomment-7082186
API Docs on rubyjs.org/doc
Section titled “API Docs on rubyjs.org/doc”You can quickly search and jump through the online documentation by using the fuzzy finder dialog:
Open fuzzy finder dialog: Ctrl-T
In frame mode you can toggle the list navigation frame on the left side:
Toggle list view: Ctrl-L
You can focus a list in frame mode or toggle a tab in frameless mode:
- Class list:
Ctrl-C
- Method list:
Ctrl-M - Extras list:
Ctrl-E
You can focus and blur the search input:
- Focus search input: `Ctrl-S
- Blur search input:
Esc - In frameless mode you can close the list tab:
Esc
Namespace
Section titled “Namespace”RubyJS is the official namespace of all classes and mixins. R is an alias to RubyJS. In the documentation both versions are used interchangeably.
RubyJS('foo')RubyJS.StringRubyJS.Array// Equivalent to:R('foo')R.StringR.ArrayR additionally includes R.Kernel, so methods defined there can be used with R.
R.puts('hello world')String
Section titled “String”RubyJS.String wraps a native String primitive.
R('foo')new R.String('foo')R.String.new('foo')R.$String(1) // Emulates Ruby String(1)Destructive methods have a _bang suffix.
str = R('foo')str.capitalize() //=> 'Foo'str //=> 'foo'str.capitalize_bang() //=> 'Foo'str //=> 'Foo'Create multiple R.Strings with R.w equivalent to Ruby %w[].
words = R.w('foo bar baz')Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
RubyJS.Array wraps a native JavaScript array. Members are not directly accessible using [] notation, use RubyJS.Array.get(1) and set(1, 'foo') instead.
Array includes optimized versions of RubyJS.Enumerable methods.
R([1,2,3]) // => an R.Array of Number primitivesR([1,2,3], true) // => an R.Array of R.Fixnumsnew R.Array([1,2,3])Enumerable, Enumerator
Section titled “Enumerable, Enumerator”The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collection.
Enumerable is currently included in Array, Range and Enumerator. Array implements optimized versions of the methods.
Numerics
Section titled “Numerics”Numeric and Integer are both modules. Functional number types are Fixnum (an Integer) and Float.
- Fixnum includes Numeric, Integer
- Float includes Numeric
Mathematic operations like +, -, * with RubyJS numerics is expensive as for every operation extra objects are created. It is suggested to use JavaScript native primitives for calculations, especially in loops.
Aliases
Section titled “Aliases”Where Ruby methods conflict with JavaScript naming the following aliases are used.
You can also use the JS names quoted inside of brackets, e.g. ['=='].
str.equals('foo')str['==']!('foo')
# '?' is removedinclude : include?
# '!'' => _bangupcase_bang: upcase!
append : <<equals : ==equal_case : ===cmp : <=>lt : <lteq : <=gt : >gteq : >=modulo : %plus : +minus : -multiply : *exp : **divide : /
# Typecasting:
R.$String(): String()R.$Float() : Float()
# Special variables
R['$~'] : $~R['$;'] : $;R['$,'] : $,Roadmap 0.8
Section titled “Roadmap 0.8”- @to_enum returns Arrays not Enumerators.
- upto, downto, times to yield primitives
Contributors
Section titled “Contributors”- typos jeanlange
- rubyjs-rails gem PikachuEXE