https://github.com/nwtgck/rubyize-python
Write Python in Ruby-like way
https://github.com/nwtgck/rubyize-python
python python-library ruby
Last synced: about 1 month ago
JSON representation
Write Python in Ruby-like way
- Host: GitHub
- URL: https://github.com/nwtgck/rubyize-python
- Owner: nwtgck
- Created: 2018-01-06T13:31:41.000Z (over 8 years ago)
- Default Branch: feature/basic
- Last Pushed: 2021-04-29T19:19:20.000Z (about 5 years ago)
- Last Synced: 2025-02-06T13:30:06.369Z (over 1 year ago)
- Topics: python, python-library, ruby
- Language: Python
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rubyize

| branch | Travis status|
| --- | --- |
| [`develop`](https://github.com/nwtgck/rubyize-python/tree/develop) | [](https://travis-ci.com/nwtgck/rubyize-python) |
Write Python in Ruby-like way
## Installation
```bash
pip3 install toml
pip3 install git+https://github.com/nwtgck/rubyize-python
```
## Usages
Here is an import-magic for adding methods to collection classes such as `list`.
```py
import rubyize
```
```py
[1, 2, 3].length()
# => 3
[1, 2, 3].map(lambda x: x*2)
# => [2, 4, 6]
[1, 2, 3].filter(lambda x: x % 2 == 0)
# => [2]
[1, 2, 3].inject(lambda s, e: s + e)
# => 6
[1, 2, 3].inject(100, lambda s, e: s + e)
# => 106
[1, 2, 3].take(2)
# => [1, 2]
[1, 2, 3].drop(2)
# => [3]
[1, 2, 3, 4, 5, 6].group_by(lambda e: e % 2 == 0)
# => {True: [2, 4, 6], False: [1, 3, 5]}
[1, 2, None, 4, None, 6].compact()
# => [1, 2, 4, 6]
[1, 2, 3].flat_map(lambda e: [e] * 2)
# => [1, 1, 2, 2, 3, 3]
[1, 2, 3, 4, 5].each_cons(3)
# => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[1, 2, 3, 4, 5].each_slice(2)
# => [[1, 2], [3, 4], [5]]
[1, 2, 3, 4, 5].join(',')
# => "1,2,3,4,5"
```
## Not only list
```py
map(lambda e: e, [1, 2, 3]).map(lambda x: x * 2)
map(lambda e: e, [1, 2, 3]).length()
map(lambda e: e, [1, 2, 3]).take(2)
...
```
## Why Rubyize?
Collection API of Ruby is very rich! Ruby has prepared a lot of methods. So, users can write easily about what they want to do. Users make method chains, which are directly connected to our brain thinking rather than built-in `len(mylist)`, `len(mylist)` and etc, I think.
## Caution
Rubyize can destroy the Python culture. I think that Python is designed for everyone writing in the same way. However, Rubyize allows users to write code in different ways. Readability in terms of the Python culture can be worse. Thus, I think Rubyize should not be used in production. Rubyize should be used in disposable code or etc.
## Forbidden Fruit
This project is powered by [Forbiddenfruit](https://github.com/clarete/forbiddenfruit).