Does Ruby have a method similar to Emacs align-regexp?
来源:互联网 发布:微电阻测试仪单片机 编辑:程序博客网 时间:2024/04/28 08:37
Looking for a Ruby method to operate on an array of strings and align them according to a regex given as as an argument. Emacs has a defun called align-regexp that does this interactively when operating on a region of a buffer.
Here is a portion of the emacs doc string for align-regexp.
For example, let's say you had a list of phone numbers, and wanted to align them so that the opening parentheses would line up:
Fred (123) 456-7890Alice (123) 456-7890Mary-Anne (123) 456-7890Joe (123) 456-7890
There is no predefined rule to handle this, but you could easily do it using a REGEXP like "(". All you would have to do is to mark the region, call `align-regexp' and type in that regular expression. Here is the result:
Fred (123) 456-7890Alice (123) 456-7890Mary-Anne (123) 456-7890Joe (123) 456-7890
lines = [ 'Fred (123) 456-7890', 'Alice (123) 456-7890', 'Mary-Anne (123) 456-7890', 'Joe (123) 456-7890',]rows = lines.map { |line| line.partition('(') }pos = rows.map { |row| row[0].size }.maxputs rows.map { |row| row[0] = row[0].ljust(pos); row.join }
output:
Fred (123) 456-7890Alice (123) 456-7890Mary-Anne (123) 456-7890Joe (123) 456-7890
0 0
- Does Ruby have a method similar to Emacs align-regexp?
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ?
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ? .
- The form bound to the class does not have a valid binding for the root component
- Re:Method does not have the same signature as delegate
- 报错: LINQ to Entities does not recognize the method...and this method cannot be translated into a sto
- use regexp() to split a string
- Why do I have hundreds of child cursors when cursor_sharing set to similar in 10g
- CSS - How to align a <div> center?
- Unable to locate the server named "localhost" --- the server does not have a DNS entry. Perhaps ther
- Error: Failed to create the site. The machine does not have a valid license. Please authorize ArcGIS
- Why does TypeScript have to be the answer to anything?
- WCF分布式开发常见错误(3):客户端调用服务出错:You have tried to create a channel to a service that does not support .Net Framing
- The application does not have a valid signature
- Text editor does not have a document provider
- any object does not have a member named generator
- DataGridView Index -1 does not have a value 错误
- Activity does not have a parent activity name specified.
- QT教程 休闲棋牌游戏开发(1)
- Python 正则表达式验证十六进制为特定范围
- linux下alsaplayer-0.99.81移植到mips架构下
- windows命令行下管道统计命令输出结果行数
- VC窗口闪烁的解决办法
- Does Ruby have a method similar to Emacs align-regexp?
- QT教程 休闲棋牌游戏开发(2)
- XAMPP环境下用phpStorm+XDebug进行断点调试的配
- Android的计量单位px,in,mm,pt,dp,dip,sp
- svn eclipse插件突然失效怎么办?
- 黑马程序员————TreeSet实例
- web页面jsp页面的打印
- LeetCode-Single Number II
- 字符编码笔记:ASCII,Unicode和UTF-8