有很多关于Python与Ruby的讨论,我发现它们完全没有用,因为它们都转向了为什么功能X在语言Y中很糟糕,或者声称语言Y没有X,尽管事实上确实如此.我也确切地知道为什么我更喜欢Python,但这也是主观的,并且不会帮助任何人选择,因为他们可能没有和我一样的开发品味.
因此,客观地列出差异将是有趣的.所以没有"Python的lambdas糟透了".而是解释Ruby的lambdas可以做什么,Python不能.没有主观性.示例代码很好!
请不要在一个答案中有几个不同之处.并且对那些你认识的是正确的进行投票,然后对你所知道的那些是不正确的(或者是主观的).此外,语法上的差异也不大.我们知道Python使用缩进来处理Ruby用括号和结尾做什么,并且@在Python中称为self.
更新:这是一个社区维基,所以我们可以在这里添加很大的差异.
在Ruby中,您可以在类主体中引用类(self).在Python中,在类构造完成之前,您没有对类的引用.
一个例子:
class Kaka
puts self
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,self就是类,这段代码会打印出"Kaka".无法打印出类名或以其他方式从Python中的类定义体(外部方法定义)访问该类.
这使您可以开发核心类的扩展.以下是rails扩展的示例:
class String
def starts_with?(other)
head = self[0, other.length]
head == other
end
end
Run Code Online (Sandbox Code Playgroud)
Python(想象没有''.startswith方法):
def starts_with(s, prefix):
return s[:len(prefix)] == prefix
Run Code Online (Sandbox Code Playgroud)
您可以在任何序列(不仅仅是字符串)上使用它.为了使用它,你应该明确地导入它,例如,from some_module import starts_with.
Ruby拥有一流的regexp,$ -variables,awk/perl逐行输入循环和其他功能,使其更适合编写munge文本文件的小shell脚本或充当其他程序的粘合代码.
感谢callcc声明.在Python中,您可以通过各种技术创建延续,但是该语言没有内置支持.
使用"do"语句,您可以在Ruby中创建一个多行匿名函数,它将作为参数传递到do前面的方法中,并从那里调用.在Python中,您可以通过传递方法或使用生成器来执行此操作.
红宝石:
amethod { |here|
many=lines+of+code
goes(here)
}
Run Code Online (Sandbox Code Playgroud)
Python(Ruby块对应于Python中的不同构造):
with amethod() as here: # `amethod() is a context manager
many=lines+of+code
goes(here)
Run Code Online (Sandbox Code Playgroud)
要么
for here in amethod(): # `amethod()` …Run Code Online (Sandbox Code Playgroud) 我试图理解块以及yield它们如何在Ruby中工作.
如何yield使用?我看过的许多Rails应用程序都是yield以一种奇怪的方式使用的.
有人可以向我解释或告诉我去哪里去理解它们吗?
是否有宝石为终端输出执行背景和前景文本着色?
我记得,在编写Pascal时,我们都习惯使用textcolor(...)程序来使我们的小型教育程序看起来更漂亮和表现.
Ruby中有类似的东西吗?
我需要一种简单的方法来获取tar文件并将其转换为字符串(反之亦然).有没有办法在Ruby中做到这一点?我最好的尝试是这样的:
file = File.open("path-to-file.tar.gz")
contents = ""
file.each {|line|
contents << line
}
Run Code Online (Sandbox Code Playgroud)
我认为这足以将它转换为字符串,但是当我尝试将它写回来时......
newFile = File.open("test.tar.gz", "w")
newFile.write(contents)
Run Code Online (Sandbox Code Playgroud)
它不是同一个文件.做ls -l显示文件的大小不同,虽然它们非常接近(打开文件显示大部分内容完好无损).我正在制作一个小错误或完全不同(但可行)的方法来实现这一目标吗?
我在Windows上运行Ruby虽然我不知道这是否应该有所作为.我想要做的就是获取当前工作目录的绝对路径.这可能来自irb吗?显然,从脚本中可以使用它File.expand_path(__FILE__)
但是从irb我尝试了以下内容并得到了"Permission denied"错误:
File.new(Dir.new(".").path).expand
Run Code Online (Sandbox Code Playgroud) 我在Windows上安装了RubyInstaller,我正在运行IMAP Sync,但我需要用它来同步数百个帐户.如果我可以通过命令行将这些变量传递给它,我可以更好地自动化整个过程.
# Source server connection info.
SOURCE_NAME = 'username@example.com'
SOURCE_HOST = 'mail.example.com'
SOURCE_PORT = 143
SOURCE_SSL = false
SOURCE_USER = 'username'
SOURCE_PASS = 'password'
# Destination server connection info.
DEST_NAME = 'username@gmail.com'
DEST_HOST = 'imap.gmail.com'
DEST_PORT = 993
DEST_SSL = true
DEST_USER = 'username@gmail.com'
DEST_PASS = 'password'
Run Code Online (Sandbox Code Playgroud) 当我bundle install在Centos 5.5上运行我的Rails 3项目时失败并出现错误:
Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3
read server certificate B: certificate verify failed
(https://bb-m.rubygems.org/gems/multi_json-1.3.2.gem)
An error occured while installing multi_json (1.3.2), and Bundler cannot continue.
Make sure that `gem install multi_json -v '1.3.2'` succeeds before bundling.
Run Code Online (Sandbox Code Playgroud)
当我尝试手动安装gem时(by gem install multi_json -v '1.3.2')它可以工作.其他几个宝石也会出现同样的问题.我使用RVM(1.12.3),ruby 1.9.2,bundler 1.1.3.
怎么解决?
当我尝试安装最新版本的指南针(https://rubygems.org/gems/compass/versions/1.0.0.alpha.17)时,出现以下错误.
ERROR: Error installing compass:
ERROR: Failed to build gem native extension.
ERROR: Error installing compass:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... yes
checking for ffi_call() in -lffi... yes
checking for ffi_prep_closure()... yes
checking for ffi_raw_call()... no
checking for rb_thread_blocking_region()... yes
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_call_without_gvl()... yes
checking for ffi_prep_cif_var()... no
creating extconf.h
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling AbstractMemory.c
compiling ArrayType.c …Run Code Online (Sandbox Code Playgroud) ruby ×10
python ×2
block ×1
bundler ×1
colors ×1
command-line ×1
compass-sass ×1
file-io ×1
irb ×1
javascript ×1
macos ×1
performance ×1
rubygems ×1
ssl ×1
string ×1
terminal ×1