Prev: IO.popen
Next: SVG generation gem? Cairo?
From: VMDD TECH on 2 Aug 2010 10:40 Hello everyone, I am new to Ruby. If there are spaces between new and (, then ruby 1.9.1p376 considers it a syntactical error. For example: File.new ("write.txt", "w+") => syntax error, unexpected ',', expecting ')' File.new ("write.txt", "w+") ^ ....: syntax error, unexpected ')', expecting $end Is this a ruby bug or is it a feature/quirk of ruby? By the way, if I write as File.new "write.txt", "w+" File.new("write2.txt", "w+") then there are no problem. Thanks, Binh
From: Joseph E. Savard on 2 Aug 2010 12:22 When using ( spaces are not required in 1.8.7 the error is (irb):1: warning: don't put space before argument parentheses In 1.9.2 SyntaxError: (irb):1: syntax error, unexpected ',', expecting ')' f = File.open ("l.txt", "w+") 1.9.2 appears the error is a little obscure.. <MANISH.local:jes> [08-02] 0 517:17 (295.6 Mb) ! irb irb(main):001:0> f = File.open ("l.txt", "w+") (irb):1: warning: don't put space before argument parentheses => #<File:l.txt> irb(main):002:0> f = File.open("l.txt", "w+") => #<File:l.txt> irb(main):003:0> f.write("<HTML></HTML>") => 13 irb(main):004:0> f.close => nil irb(main):005:0> exit <MANISH.local:jes> [08-02] 0 518:18 (295.6 Mb) ! cat l.txt <HTML></HTML> > From: VMDD TECH <binhph(a)gmail.com> > Organization: http://groups.google.com > Reply-To: <ruby-talk(a)ruby-lang.org> > Newsgroups: comp.lang.ruby > Date: Mon, 2 Aug 2010 23:45:09 +0900 > To: ruby-talk ML <ruby-talk(a)ruby-lang.org> > Subject: File.new ("write.txt", "w+") results in error > > Hello everyone, > > I am new to Ruby. > > If there are spaces between new and (, then ruby 1.9.1p376 considers > it a syntactical error. For example: > > File.new ("write.txt", "w+") > > => > syntax error, unexpected ',', expecting ')' > File.new ("write.txt", "w+") > ^ > ...: syntax error, unexpected ')', expecting $end > > Is this a ruby bug or is it a feature/quirk of ruby? > > By the way, if I write as > File.new "write.txt", "w+" > File.new("write2.txt", "w+") > > then there are no problem. > > Thanks, > > Binh >
From: Justin Collins on 2 Aug 2010 23:15 On 08/02/2010 07:45 AM, VMDD TECH wrote: > Hello everyone, > > I am new to Ruby. > > If there are spaces between new and (, then ruby 1.9.1p376 considers > it a syntactical error. For example: > > File.new ("write.txt", "w+") > > => > syntax error, unexpected ',', expecting ')' > File.new ("write.txt", "w+") > ^ > ...: syntax error, unexpected ')', expecting $end > > Is this a ruby bug or is it a feature/quirk of ruby? > > By the way, if I write as > File.new "write.txt", "w+" > File.new("write2.txt", "w+") > > then there are no problem. > > Thanks, > > Binh > > It's not a bug. A comma-separated list inside of parentheses is just meaningless in Ruby, which is why you get a syntax error. Parentheses being "optional" for method calls is a feature, however. -Justin
From: Brian Candler on 4 Aug 2010 05:51 Eugen Ciur wrote: > File.new ("write.txt", "w+") > > => > syntax error, unexpected ',', expecting ')' > File.new ("write.txt", "w+") > ^ > ...: syntax error, unexpected ')', expecting $end > > Is this a ruby bug or is it a feature/quirk of ruby? The latter, because in ruby the parentheses surrounding the argument list are optional. Consider the difference here: puts (2-3).abs ## means: puts((2-3).abs) puts(2-3).abs ## means: (puts(2-3)).abs [The latter fails, because puts returns nil, and nil.abs is not defined] So if you include a space, the parentheses are considered part of the first argument. If you don't include a space, the parentheses are wrapping the argument list. In ruby 1.8, puts (2,3) ## accepted with warning puts (2,3).abs ## syntax error In ruby 1.9, they are both treated as a syntax error. -- Posted via http://www.ruby-forum.com/.
|
Pages: 1 Prev: IO.popen Next: SVG generation gem? Cairo? |