Hacker News 中文摘要

RSS订阅

Rubish:用纯Ruby编写的Unix shell -- Rubish: A Unix shell written in pure Ruby

文章摘要

Rubish是一个用纯Ruby编写的UNIX shell,完全兼容Bash语法并支持现有脚本运行。其特色在于深度集成Ruby功能,允许在shell脚本中无缝混合使用Ruby代码、块、迭代器等特性。可通过Homebrew或源码安装,提供自动查找Ruby环境的启动器。

文章总结

Rubish - 基于Ruby的UNIX Shell

项目概述

Rubish是一个用纯Ruby编写的UNIX shell,它将shell语法解析并编译为Ruby代码,然后由Ruby虚拟机执行。

核心特性

Bash完全兼容

  • 支持所有bash功能,语法完全兼容
  • 可直接运行现有bash脚本
  • 如发现不兼容情况,视为bug欢迎报告

深度Ruby集成

  • 不仅是Ruby实现的shell,更是深度集成Ruby的shell
  • 可无缝混合shell命令和Ruby代码
  • 支持使用Ruby的块、迭代器和库等强大功能

安装方式

Homebrew安装(macOS)

bash brew tap amatsuda/rubish brew install --HEAD rubish

源码安装

bash git clone https://github.com/amatsuda/rubish.git cd rubish bundle install bundle exec exe/rubish

使用方式

基础使用

  • 启动交互式shell:rubish
  • 执行单条命令:rubish -c 'echo hello'
  • 运行脚本:rubish script.sh
  • 可设置为登录shell

设置为登录shell

bash echo "$(which rubish)" | sudo tee -a /etc/shells chsh -s "$(which rubish)"

超越Bash的特性

Ruby条件表达式

ruby COUNT=5 if { count.to_i > 3 } echo 'count大于3' end

Ruby方法调用风格

```ruby

传统UNIX风格

ls -la

Ruby方法调用风格

ls('-la') ```

方法链式调用

ruby ls().sort ls().sort.uniq cat(file.txt).grep(/error/)

Ruby迭代器块

ruby ls.each { |f| puts f.upcase } cat(file.txt).map { |line| line.strip } ls.select { it.end_with?('.rb') }

内联Ruby求值

任何以大写字母开头的行会被直接作为Ruby代码求值: ruby Time.now Dir.glob('*.rb').sort ENV['HOME']

配置与扩展

自定义Ruby提示符

ruby def rubish_prompt branch = `git branch --show-current 2>/dev/null`.strip dir = Dir.pwd.sub(ENV['HOME'], '~') "\e[36m#{dir}\e[0m \e[33m#{branch}\e[0m $ " end

延迟加载

ruby lazy_load { `rbenv init - --no-rehash bash` }

嵌入Ruby程序

Rubish提供公开API,允许其他Ruby程序驱动rubish会话: ruby require 'rubish' repl = Rubish::REPL.new(login_shell: true) repl.run

开发与贡献

  • 开发环境搭建:bundle install && bundle exec rake test
  • 欢迎提交bug报告和pull request到GitHub仓库

许可证

采用MIT许可证

评论总结

评论总结:

  1. 项目命名获得广泛好评
  • "Oh my god this is the best name to the application. You win the weekend." (评论3)
  • "Very clever name!" (评论16)
  • "A+ name, no notes" (评论19)
  1. 对Ruby语言性能的讨论
  • "Ruby is a slower language than Go. But ruby these days is faster than Python." (评论4)
  • "This is probably even slower than bash" (评论18)
  1. 项目创新性与实用性的评价 正面观点:
  • "actually promotes a deep integration with its language of choice" (评论7)
  • "a fascinating project...great work!" (评论13)

质疑观点: - "The idea is also not quite new" (评论6) - "Missing: size, speed comparison" (评论18)

  1. 代码质量的讨论
  • "the code itself makes it harder for people to contribute" (评论12)
  • "Where are the interface boundaries? Why are there methods that are 200 lines long?" (评论12)
  1. 与其他语言的比较
  • "rc (plan9 shell): 5,888...rubish: 26,842" (评论9)
  • "I write a horrifying amount of PowersHell...just 'bash but also with objects'" (评论15)
  1. 关于AI辅助开发的讨论
  • "I don't know if this sits on the thin line between a vibe coded project and an LLM assisted project" (评论10)