Ruby中chr()的反义词是什么? [英] What's the opposite of chr() in Ruby?

查看:106
本文介绍了Ruby中chr()的反义词是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多语言中,有一对函数chr()ord(),可在数字和字符值之间转换.在某些语言中,ord()被称为asc().

In many languages there's a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord() is called asc().

Ruby的Integer#chr效果很好:

Ruby has Integer#chr, which works great:

>> 65.chr
A

足够公平.但是你怎么走呢?

Fair enough. But how do you go the other way?

"A".each_byte do |byte|
   puts byte
end

打印:

65

这与我想要的非常接近.但我确实希望避免循环-我在声明const时正在寻找足够短的内容以使其易于阅读.

and that's pretty close to what I want. But I'd really rather avoid a loop -- I'm looking for something short enough to be readable when declaring a const.

推荐答案

如果String#ord在1.9中不存在,则在2.0中存在:

If String#ord didn't exist in 1.9, it does in 2.0:

"A".ord #=> 65

这篇关于Ruby中chr()的反义词是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆