Which is better for beginning programmers: Ruby or Python?
What does Ruby have that Python doesn't, and vice versa? - Stack Overflow
Ruby vs. Python comes down to the for loop
Your python example is unnecessarily complicated. You can achieve the exact same thing just as simple as you would in your Ruby example. Perhaps you are not aware of the existence of "yield" in python?
class Stuff:
def __init__(self):
self.a_list = [1, 2, 3, 4]
def __iter__(self):
for val in self.a_list:
yield val More on reddit.com Python vs Ruby! Which is easier?
Having used both, I'd say Ruby, especially if you're already an experienced programmer who's more used to C syntax. Python feels restrictive to me, Ruby has powers that reveal themselves as you learn more. But neither are my preferred language.
More on reddit.comWhich programming language is better: Ruby or Python?
Is Ruby better than Python?
Are Ruby and Python similar?
Videos
Strictly between these two. Excluding their uses (Ruby mostly used for web dev, Python mostly used for AI/ML) which is more beginner friendly for new programmers, considering their syntax and features?
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.
Python Example
Functions are first-class variables in Python. You can declare a function, pass it around as an object, and overwrite it:
def func(): print "hello"
def another_func(f): f()
another_func(func)
def func2(): print "goodbye"
func = func2
This is a fundamental feature of modern scripting languages. JavaScript and Lua do this, too. Ruby doesn't treat functions this way; naming a function calls it.
Of course, there are ways to do these things in Ruby, but they're not first-class operations. For example, you can wrap a function with Proc.new to treat it as a variable--but then it's no longer a function; it's an object with a "call" method.
Ruby's functions aren't first-class objects
Ruby functions aren't first-class objects. Functions must be wrapped in an object to pass them around; the resulting object can't be treated like a function. Functions can't be assigned in a first-class manner; instead, a function in its container object must be called to modify them.
def func; p "Hello" end
def another_func(f); method(f)[] end
another_func(:func) # => "Hello"
def func2; print "Goodbye!"
self.class.send(:define_method, :func, method(:func2))
func # => "Goodbye!"
method(:func).owner # => Object
func # => "Goodbye!"
self.func # => "Goodbye!"