X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=blobdiff_plain;f=shogi_server%2Futil.rb;h=ebf8c9d1148b9f472a05bff579862052478c2993;hp=b3141e6dbc997de610767c23de4126d25b362150;hb=3c5c117c78c14049e32df336a0a4105a4e3a9ead;hpb=d31194521135876378c1a02abe2b66b783a7a30c diff --git a/shogi_server/util.rb b/shogi_server/util.rb index b3141e6..ebf8c9d 100644 --- a/shogi_server/util.rb +++ b/shogi_server/util.rb @@ -17,11 +17,14 @@ ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +require 'fileutils' +require 'pathname' + module ShogiServer # Generate a random number such as i<=n= max + return i if i >= max return rand(max-i)+i end module_function :random @@ -37,4 +40,31 @@ module ShogiServer end module_function :shuffle + # See if the file is writable. The file will be created if it does not exist + # yet. + # Return true if the file is writable, otherwise false. + # + def is_writable_file?(file) + if String === file + file = Pathname.new file + end + if file.exist? + if file.file? + return file.writable_real? + else + return false + end + end + + begin + file.open("w") {|fh| } + file.delete + rescue + return false + end + + return true + end + module_function :is_writable_file? + end