language-comparisons

Ruby equivalent for Python's "try"?

Ruby equivalent for Python's "try"? Question: I’m trying to convert some Python code into Ruby. Is there an equivalent in Ruby to the try statement in Python? Asked By: thatonegirlo || Source Answers: begin some_code rescue handle_error ensure this_code_is_always_executed end Details: http://crodrigues.com/try-catch-finally-equivalent-in-ruby/ Answered By: zengr Use this as an example: begin # "try" block puts …

Total answers: 3

Does Python do variable interpolation similar to "string #{var}" in Ruby?

Does Python do variable interpolation similar to "string #{var}" in Ruby? Question: In Python, it is tedious to write: print “foo is” + bar + ‘.’ Can I do something like this in Python? print “foo is #{bar}.” Asked By: mko || Source Answers: Yes, absolutely. Python, in my opinion, has great support for string …

Total answers: 9

Python: Assign Value if None Exists

Python: Assign Value if None Exists Question: I am a RoR programmer new to Python. I am trying to find the syntax that will allow me to set a variable to a specific value only if it wasn’t previously assigned. Basically I want: # only if var1 has not been previously assigned var1 = 4 …

Total answers: 9

What is the equivalent of a Python docstring in Ruby?

What is the equivalent of a Python docstring in Ruby? Question: In Python, you can access an object’s docstring by using obj.__doc__. What is the equivalent action in Ruby? Asked By: parzival || Source Answers: I don’t believe ruby supports this. Answered By: Olly Ruby does not have a Python __doc__ equivalent. They often use …

Total answers: 4

Make Javascript do List Comprehension

Make Javascript do List Comprehension Question: What is the cleanest way to make Javascript do something like Python’s list comprehension? In Python if I have a list of objects whose name’s I want to ‘pull out’ I would do this… list_of_names = [x.name for x in list_of_objects] In javascript I don’t really see a more …

Total answers: 11

Is there a Python equivalent to Ruby's string interpolation?

Is there a Python equivalent to Ruby's string interpolation? Question: Ruby example: name = “Spongebob Squarepants” puts “Who lives in a Pineapple under the sea? n#{name}.” The successful Python string concatenation is seemingly verbose to me. Asked By: Caste || Source Answers: Python 3.6 will add literal string interpolation similar to Ruby’s string interpolation. Starting …

Total answers: 9

Is there a Python equivalent to Ruby symbols?

Is there a Python equivalent to Ruby symbols? Question: Is there a Python equivalent to Ruby symbols? If so then what is it? If not then are we stuck with using strings as our keys in dictionaries only? Asked By: pylonicon || Source Answers: No, there is no equivalent. No you can use every hashable …

Total answers: 5

What is the equivalent in PHP for Python's pass statement?

What is the equivalent in PHP for Python's pass statement? Question: Do you know any PHP statement that works like Python’s pass statement? Asked By: Jan Tojnar || Source Answers: Just leave the bracket’s empty… Python has the pass word because they don’t use brackets to define the body part of classes, function, and other …

Total answers: 6

What is the Bash equivalent of Python's pass statement

What is the Bash equivalent of Python's pass statement Question: Is there a Bash equivalent to the Python’s pass statement? Asked By: skeept || Source Answers: You can use : for this. Answered By: Ignacio Vazquez-Abrams true is a command that successfully does nothing. (false would, in a way, be the opposite: it doesn’t do …

Total answers: 2

PHP equivalent of Python's __name__ == "__main__"?

PHP equivalent of Python's __name__ == "__main__"? Question: As per the title, is there PHP equivalent of __name__ == “__main__”? Is there something that would work for both scripts executed through the command line and through a web request, or would a custom function be needed? For those unfamiliar with Python, __name__ == “__main__” allows …

Total answers: 4