Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Not 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")


Python:

with open("filename", "r") as a_file:

    contents = a_file.read()


Ruby 1.9.3 added File.write("filename", "some data").


Cool. Didn't know that. Thanks.


I 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)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: