Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 307 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does Ruby have that Python doesn't, and vice versa?

#1
There is a lot of discussions of Python vs Ruby, and I all find them completely unhelpful, because they all turn around why feature X sucks in language Y, or that claim language Y doesn't have X, although in fact it does. I also know exactly why I prefer Python, but that's also subjective, and wouldn't help anybody choosing, as they might not have the same tastes in development as I do.

It would therefore be interesting to list the differences, objectively. So no "Python's lambdas sucks". Instead explain what Ruby's lambdas can do that Python's can't. No subjectivity. Example code is good!


Don't have several differences in one answer, please. And vote up the ones you know are correct, and down those you know are incorrect (or are subjective). Also, differences in syntax is not interesting. We know Python does with indentation what Ruby does with brackets and ends, and that @ is called self in Python.


UPDATE: This is now a community wiki, so we can add the big differences here.


## Ruby has a class reference in the class body ##

In Ruby you have a reference to the class (self) already in the class body. In Python you don't have a reference to the class until after the class construction is finished.

An example:

class Kaka
puts self
end

self in this case is the class, and this code would print out "Kaka". There is no way to print out the class name or in other ways access the class from the class definition body in Python (outside method definitions).


## All classes are mutable in Ruby ##

This lets you develop extensions to core classes. Here's an example of a rails extension:

class String
def starts_with?(other)
head = self[0, other.length]
head == other
end
end

Python (imagine there were no `''.startswith` method):

def starts_with(s, prefix):
return s[:len(prefix)] == prefix

You could use it on any sequence (not just strings). In order to use it you should import it *explicitly* e.g., `from some_module import starts_with`.

## Ruby has Perl-like scripting features ##

Ruby has first class regexps, $-variables, the awk/perl line by line input loop and other features that make it more suited to writing small shell scripts that munge text files or act as glue code for other programs.

## Ruby has first class continuations ##

Thanks to the callcc statement. In Python you can create continuations by various techniques, but there is no support built in to the language.

## Ruby has blocks ##

With the "do" statement you can create a multi-line anonymous function in Ruby, which will be passed in as an argument into the method in front of do, and called from there. In Python you would instead do this either by passing a method or with generators.

Ruby:

amethod { |here|
many=lines+of+code
goes(here)
}

Python (Ruby blocks correspond to different constructs in Python):

with amethod() as here: # `amethod() is a context manager
many=lines+of+code
goes(here)

Or

for here in amethod(): # `amethod()` is an iterable
many=lines+of+code
goes(here)

Or

def function(here):
many=lines+of+code
goes(here)

amethod(function) # `function` is a callback


Interestingly, the convenience statement in Ruby for calling a block is called "yield", which in Python will create a generator.

Ruby:

def themethod
yield 5
end

themethod do |foo|
puts foo
end

Python:

def themethod():
yield 5

for foo in themethod():
print foo

Although the principles are different, the result is strikingly similar.

## Ruby supports functional style (pipe-like) programming more easily ##

myList.map(&:description).reject(&:empty?).join("\n")

Python:

descriptions = (f.description() for f in mylist)
"\n".join(filter(len, descriptions))


## Python has built-in generators (which are used like Ruby blocks, as noted above) ##

Python has support for generators in the language. In Ruby 1.8 you can use the generator module which uses continuations to create a generator from a block. Or, you could just use a block/proc/lambda! Moreover, in Ruby 1.9 Fibers are, and can be used as, generators, and the Enumerator class is a built-in generator [4]

[docs.python.org][1] has this generator example:

def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]

Contrast this with the above block examples.


## Python has flexible name space handling ##

In Ruby, when you import a file with `require`, all the things defined in that file will end up in your global namespace. This causes namespace pollution. The solution to that is Rubys modules. But if you create a namespace with a module, then you have to use that namespace to access the contained classes.

In Python, the file is a module, and you can import its contained names with `from themodule import *`, thereby polluting the namespace if you want. But you can also import just selected names with `from themodule import aname, another` or you can simply `import themodule` and then access the names with `themodule.aname`. If you want more levels in your namespace you can have packages, which are directories with modules and an `__init__.py` file.

## Python has docstrings ##

Docstrings are strings that are attached to modules, functions and methods and can be
introspected at runtime. This helps for creating such things as the help command and
automatic documentation.

def frobnicate(bar):
"""frobnicate takes a bar and frobnicates it

>>> bar = Bar()
>>> bar.is_frobnicated()
False
>>> frobnicate(bar)
>>> bar.is_frobnicated()
True
"""

Ruby's equivalent are similar to javadocs, and located above the method instead of within it. They can be retrieved at runtime from the files by using 1.9's Method#source_location [example use][2]

## Python has multiple inheritance ##

Ruby does not ("on purpose" -- see Ruby's website, [see here how it's done in Ruby][3]). It does reuse the module concept as a type of abstract classes.

## Python has list/dict comprehensions ##

Python:

res = [x*x for x in range(1, 10)]

Ruby:

res = (0..9).map { |x| x * x }

Python:

>>> (x*x for x in range(10))
<generator object <genexpr> at 0xb7c1ccd4>
>>> list(_)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Ruby:

p = proc { |x| x * x }
(0..9).map(&p)

Python **2.7+**:

>>> {x:str(y*y) for x,y in {1:2, 3:4}.items()}
{1: '4', 3: '16'}

Ruby:

>> Hash[{1=>2, 3=>4}.map{|x,y| [x,(y*y).to_s]}]
=> {1=>"4", 3=>"16"}

## Python has decorators ##

Things similar to decorators can also be created in Ruby, and it can also be argued that they aren't as necessary as in Python.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

"example use"
[3]:

[To see links please register here]

[4]:

[To see links please register here]

## Syntax differences ##

Ruby requires "end" or "}" to close all of its scopes, while Python uses white-space only. There have been recent attempts in Ruby to allow for whitespace only indentation

[To see links please register here]

Reply

#2
Ruby has the concepts of _blocks_, which are essentially syntactic sugar around a section of code; they are a way to create closures and pass them to another method which may or may not use the block. A block can be invoked later on through a `yield` statement.

For example, a simple definition of an `each` method on `Array` might be something like:

class Array
def each
for i in self
yield(i) # If a block has been passed, control will be passed here.
end
end
end

Then you can invoke this like so:

# Add five to each element.
[1, 2, 3, 4].each{ |e| puts e + 5 }
> [6, 7, 8, 9]

Python has anonymous functions/closures/lambdas, but it doesn't quite have blocks since it's missing some of the useful syntactic sugar. However, there's at least one way to get it in an ad-hoc fashion. See, for example, [**here**][1].


[1]:

[To see links please register here]

Reply

#3
What Ruby has over Python are its scripting language capabilities. Scripting language in this context meaning to be used for "glue code" in shell scripts and general text manipulation.

These are mostly shared with Perl. First-class built-in regular expressions, $-Variables, useful command line options like Perl (-a, -e) etc.

Together with its terse yet epxressive syntax it is perfect for these kind of tasks.

Python to me is more of a dynamically typed business language that is very easy to learn and has a neat syntax. Not as "cool" as Ruby but neat.
What Python has over Ruby to me is the vast number of bindings for other libs. Bindings to Qt and other GUI libs, many game support libraries and and and. Ruby has much less. While much used bindings e.g. to Databases are of good quality I found niche libs to be better supported in Python even if for the same library there is also a Ruby binding.

So, I'd say both languages have its use and it is the task that defines which one to use. Both are easy enough to learn. I use them side-by-side. Ruby for scripting and Python for stand-alone apps.
Reply

#4
While the functionalty is to a great extent the same (especially in the [Turing][1] sense), malicious tongues claim that Ruby was created for Pythonistas that could not split up with the Perlish coding style.

[1]:

[To see links please register here]

Reply

#5
Python has a "we're all adults here" mentality. Thus, you'll find that Ruby has things like constants while Python doesn't (although Ruby's constants only raise a warning). The Python way of thinking is that if you want to make something constant, you should put the variable names in all caps and not change it.

For example, Ruby:

>> PI = 3.14
=> 3.14
>> PI += 1
(irb):2: warning: already initialized constant PI
=> 4.14

Python:

>>> PI = 3.14
>>> PI += 1
>>> PI
4.1400000000000006

Reply

#6
I don't think "Ruby has X and Python doesn't, while Python has Y and Ruby doesn't" is the most useful way to look at it. They're quite similar languages, with many shared abilities.

To a large degree, the difference is what the language makes elegant and readable. To use an example you brought up, both do theoretically have lambdas, but Python programmers tend to avoid them, and constructs made using them do not look anywhere near as readable or idiomatic as in Ruby. So in Python, a good programmer will want to take a different route to solving the problem than he would in Ruby, just because it actually *is* the better way to do it.
Reply

#7
Ruby has builtin continuation support using `callcc`.

Hence you can implement cool things like the [amb-operator][1]


[1]:

[To see links please register here]

Reply

#8
**Some others from:**

[To see links please register here]


(If I have misintrepreted anything or any of these have changed on the Ruby side since that page was updated, someone feel free to edit...)

Strings are mutable in Ruby, not in Python (where new strings are created by "changes").

Ruby has some enforced case conventions, Python does not.

Python has both lists and tuples (immutable lists). Ruby has arrays corresponding to Python lists, but no immutable variant of them.

In Python, you can directly access object attributes. In Ruby, it's always via methods.

In Ruby, parentheses for method calls are usually optional, but not in Python.

Ruby has public, private, and protected to enforce access, instead of Python’s convention of using underscores and name mangling.

Python has multiple inheritance. Ruby has "mixins."


**And another very relevant link:**

[To see links please register here]


Which, in particular, links to **another good one by Alex Martelli**, who's been also posting a lot of great stuff here on SO:

[To see links please register here]

Reply

#9
Python has docstrings and ruby doesn't... Or if it doesn't, they are not accessible as easily as in python.


Ps. If im wrong, pretty please, leave an example? I have a workaround that i could monkeypatch into classes quite easily but i'd like to have docstring kinda of a feature in "native way".
Reply

#10
Ruby has a line by line loop over input files (the '-n' flag) from the commandline so it can be used like AWK. This Ruby one-liner:

ruby -ne 'END {puts $.}'

will count lines like the AWK one-liner:

awk 'END{print NR}'

Ruby gets feature this through Perl, which took it from AWK as a way of getting sysadmins on board with Perl without having to change the way they do things.


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through