3 Replies - 13593 Views - Last Post: 29 August 2011 - 11:13 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Rails - if or (||) conditional statement:

Posted 28 August 2011 - 02:59 PM

I'm having trouble writing this kind of condition in one of my controllers. It gives me the error...

[error]
syntax error, unexpected tSTRING_BEG, expecting keyword_then or ';' or '\n'
if @solver.include? ';' || @solver.include? ' '
[/error]

  def create
    @solver = params[:solver][:param]
    
    if @solver.include? ';'  || @solver.include? ' '
      puts "Hax0rz attempt detected"
      return false
    end
    .
    .
    .



That fails, but when I write it out long hand, everything works fine. What gives?


Here's the entire method

  # POST /solvers
  # POST /solvers.xml
  def create
    #@solver = Solver.new(params[:solver])
    
    @solver = params[:solver][:param]
    
    if @solver.include? ';'  || @solver.include? ' '
      puts "Hax0rz attempt detected"
      return false
    end
    
    respz = %x[cd CODE;./Sudoku.rb #{@solver}]
    
    time_start = respz.index(":") + 2
    new_line_loc = respz.index("\n")
    
    time_end = new_line_loc - 21
    
    timez = respz[time_start, time_end]
    
    
    grid_start = respz.index(",") - 1
    
    respz = respz[grid_start, respz.length].gsub("\n", ",").chop
    
    
    
    @solver = { :time => timez, :grid => respz }
    
    respond_to do |format|
    
      #format.html { redirect_to(@solver, :notice => 'Solver was successfully created.') }
      #format.js { render :inline => 'hi' }
      format.js { render :json => @solver }
      #format.xml  { render :xml => @solver, :status => :created, :location => @solver }
    end
  end


This post has been edited by NotarySojac: 28 August 2011 - 03:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rails - if or (||) conditional statement:

#2 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Rails - if or (||) conditional statement:

Posted 28 August 2011 - 04:35 PM

You could always try using regex to check for a whitespace or semi-colon. Perhaps something like:

if @solver =~ /\s|\;/
  puts "Hax0rz attempt detected"
  return false
end



The problem with || and or are their precedence. I believe that by using || the statement translates to if @solver.include? (';' or @solver.include? ' ') where as using the word 'or' would translate to if (@solver.include? ';') or @solver.include? ' '

Hope that makes sense.
Was This Post Helpful? 2
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Rails - if or (||) conditional statement:

Posted 29 August 2011 - 10:48 AM

Thanks for the info, Skaggles!

    if @solver.include? ' ' or @solver.include? ';'
      puts "Hax0rz attempt detected"
      return false
    end



works as expected, but oddly, not
    if @solver.include? ' ' || ';'
      puts "Hax0rz attempt detected"
      return false
    end



But the regex method is probably best, and I've been meaning to use regex more often due to its conciseness and power.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Rails - if or (||) conditional statement:

Posted 29 August 2011 - 11:13 AM

@solver.include? ' ' || ';' does not work because ' ' || ';' simply evaluates to ' ' (x || y being equivalent to if x then x else y end), thus completely ignoring the ';'.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1