Does Ruby have a dir method, similar to Python?
There is fairly popular gem pry, which has method called ls
that can provide you I believe with something, similar to the dir
in Python.
Python's equivalent of Ruby's ||=
I think there is some confusion from the people who aren't really sure what the conditional assignment operator (||=
) does, and also some misunderstanding about how variables are spawned in Ruby.
Everyone should read this article on the subject. A TLDR quote:
A common misconception is that a ||= b is equivalent to a = a || b, but it behaves like a || a = b
In a = a || b, a is set to something by the statement on every run, whereas with a || a = b, a is only set if a is logically false (i.e. if it's nil or false) because || is 'short circuiting'. That is, if the left hand side of the || comparison is true, there's no need to check the right hand side.
And another very important note:
...a variable assignment, even if not run, immediately summons that variable into being.
# Ruby
x = 10 if 2 == 5
puts x
Even though the first line won't be run, x will exist on the second line and no exception will be raised.
This means that Ruby will absolutely ensure that there is a variable container for a value to be placed into before any righthand conditionals take place. ||=
doesn't assign if a
is not defined, it assigns if a
is falsy (again, false
or nil
- nil
being the default nothingness value in Ruby), whilst guaranteeing a
is defined.
What does this mean for Python?
Well, if a
is defined, the following:
# Ruby
a ||= 10
is actually equivalent to:
# Python
if not a:
a = 10
while the following:
# Either language
a = a or 10
is close, but it always assigns a value, whereas the previous examples do not.
And if a
is not defined the whole operation is closer to:
# Python
a = None
if not a:
a = 10
Because a very explicit example of what a ||= 10
does when a
is not defined would be:
# Ruby
if not defined? a
a = nil
end
if not a
a = 10
end
At the end of the day, the ||=
operator is not completely translatable to Python in any kind of 'Pythonic' way, because of how it relies on the underlying variable spawning in Ruby.
What's the Ruby equivalent of Python's os.walk?
The following will print all files recursively. Then you can use File.directory? to see if the it is a directory or a file.
Dir['**/*'].each { |f| print f }
Ruby's counterpart to python 's __init__
You can't import directories in Ruby. If you're running require 'core/utils'
, then there is a file called core/utils.rb
. There might also be a directory called core/utils/
but it's utils.rb
that is being included via require
, and it will in turn likely includes files out of core/utils/
.
So, the answer to your question is: No, there is no equivalent to foo/__init__.py
, but the same thing can typically be accomplished with foo.rb
which includes additional files in foo/
.
Is there a ruby equivalent of python -i?
irb -r hello.rb
Ruby equivalent to Python chain()
Since Ruby 2.6: if it is Enumerable, you can chain it: (example from the docs, chaining a Range to an Array)
e = Enumerator::Chain.new(1..3, [4, 5])
e.to_a #=> [1, 2, 3, 4, 5]
e.size #=> 5
Ruby equivalent to Python's help()?
It's definitely a poor cousin to iPython's help, and one of the main features I miss after moving to Ruby, but you can also use ri
from within irb
. I'd recommend the wirble gem as an easy way to set this up.
Related Topics
Is This Bad Programming Practice in Tkinter
How Find Specific Data Attribute from HTML Tag in Beautifulsoup4
Getting Segmentation Fault Core Dumped Error While Importing Robjects from Rpy2
Learning Python from Ruby; Differences and Similarities
Does Ruby Support Conditional Regular Expressions
How to Print Variable and String on Same Line in Python
How to Pass Optional Parameters to a Function
Python: Maximum Recursion Depth Exceeded
Convert Decimal Mark When Reading Numbers as Input
Pyqt Showing Video Stream from Opencv
Error Message: 'Chromedriver' Executable Needs to Be Path
Beautifulsoup Not Grabbing Dynamic Content
How to Add Sum to Zero Constraint to Glm in Python
Interpreting a Benchmark in C, Clojure, Python, Ruby, Scala and Others
How to Import a JSON from a File on Cloud Storage to Bigquery