4 ms·
In Ruby: # Save a file f.write(str) # Read a file f.read # Trim whitespace " foo ".strip # Print anything puts 1 # even numb
by egonschiele 14y ago
In Ruby:
# Save a file
f.write(str)
# Read a file
f.read
# Trim whitespace
" foo ".strip
# Print anything
puts 1 # even numbers!
puts File.open("foo", "w+") # even file handles!
puts [1, 2, 3, 4] # even arrays!
puts [].class # even classes!
puts [].method(:size) # even functions!
# Dump an array (see above)
Python is similarly clear:
# Save a file
f.write(str)
# Read a file
f.read()
# Trim whitespace
" foo ".strip()
# Print anything
print 1 # even numbers!
print open("foo", "w+") # even file handles!
print [1, 2, 3, 4] # even arrays!
print [].__class__ # even classes!
puts [].remove # even functions!
# Dump an array (see above)
Ruby and Python are extremely well documented. They have great standard libraries which wrap existing C functions too...and wrap them in a consistent fashion. They are also the second and third most popular language on Github, respectively...which means tons of great libraries to solve almost any problem you can think of.
Ruby and Python are just as easy to use as PHP.
- getsat 14y agoNot sure about the Python ones, but the Ruby read/write file examples aren't quite fully done/correct here: # Equivalent of file_put_contents() in PHP # This could also be used for appending if the correct mode is specified. File.open("filename", "w") { |f| f.write("some data") } # Equivalent of file_get_contents() in PHP some_var = File.read("filename")
- codesuela 14y agoPython: with open("filename", "r") as a_file: contents = a_file.read()
- jeltz 14y agoRuby 1.9.3 added File.write("filename", "some data").
- getsat 14y agoCool. Didn't know that. Thanks.
- egonschiele 14y agoI guess I should have specified "where f is a file handle": f = File.open("foo", "r") f.read f = File.open("foo", "w+") f.write(x)