tags:

views:

103

answers:

4

I have the following as part of a class

def to_s
   i = 0
   first_line? = true
   output = ''
   @selections.each do | selection |
      i += 1
      if first_line?
         output << selection.to_s(first_line?)
         first_line? = false
      else
         output << selection.to_s
      end
      if i >= 5
         output << "\r"
         i = 0
      else (output << " $ ")
      end
   end
   return output
end 

And i am getting the following syntax errors

SyntaxError: list2sel.rb:45: syntax error, unexpected '='
    first_line? = true
                 ^
list2sel.rb:47: syntax error, unexpected keyword_do_block, expecting keyword_end
    @selections.each do | selection |
                       ^
list2sel.rb:51: syntax error, unexpected '='
        first_line? = false
                     ^

What give, also thanks in advance, this is driving me nuts.

+4  A: 

I suppose, you can't name variables with '?' at the end.

Nakilon
+3  A: 

I believe this is a more concise way of doing the above (untested):

def to_s 
  output = ""
  @selections.each_with_index do | selection,line |  
    output << line==0 ? selection.to_s(true) and next : selection.to_s
    output << line % 5 ? " $ " : "\r"
  end 
  return output 
end

If you are not a fan of the ternary operator (x ? y : z) then you can make them ifs:

def to_s 
  output = ""
  @selections.each_with_index do | selection,line |  
    if line==0
      output << selection.to_s(true)
    else
      output << selection.to_s
      if line % 5
        output << " $ "
      else
        output << "\r"
      end
    end
  end
  return output 
end  
Mark Thomas
I like this, but there needs to add parentheses ie ( index == 0 ? selection.to_s(true) : selection.to_s ) and ( index % 5 ? " $ " : "\r" )
Ben
+3  A: 

Variable names (with a few exceptions noted below) can only contain letters, numbers and the underscore. (Also, they must begin with a letter or the underscore; they can't begin with a number.) You can't use ? or ! in a variable name.

Beyond that rule, there is a strong convention in Ruby that a question mark at the end of something indicates a method that returns a boolean value:

4.nil? # => returns false....

So even if you could use it, a variable like first_line? would confuse (and then annoy) the hell out of Rubyists. They would expect it be a method testing whether something was the first line of something (whatever exactly that means in context).

Exceptions about variable names:

  • Global variables begin with $ - e.g., $stdin for standard input.
  • Instance variables begin with @ - e.g. @name for an object
  • Class variables begin with @@ - e.g. @@total for a class
Telemachus
is there a standard syntactical way of defining voolean variables?
Ben
With non ASCII letters, you can put all kinds of weird stuff into a variable name.
Andrew Grimm
Let me ask it another way, Do rubyists prefer a specific way of defining variables that will be boolian such that the reader is clued into the fact that they are boolian. For example, in Objective-C you see isFoo in alot of places.
Ben
@Andrew I wasn't aware of that. It doesn't sound like a good idea, but thanks for the clarification.
Telemachus
@Ben I don't know of a convention about variables storing booleans in Ruby. There may be one, but it doesn't leap to mind.
Telemachus
A: 

Variable names allow non-ASCII letters, and there are non-ASCII versions of the question mark, so you can put question marks (and also some forms of space characters) into variable names.

Andrew Grimm
Again, the clarification is helpful, but I would argue that there's a huge difference (in this case) between what you *can* do and what you *should* do.
Telemachus