70089e712ed13abb99bec89bd5bfba77

i like this code, but exist another way more 'rubyst' to write it?

1
2
3
4
5
6
7
  def is_pos_activation?
    if session[:pos_activation]
      block_given? ? yield : true
    else
      false
    end
  end

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

July 22, 2010, July 22, 2010 00:06, permalink

1 rating. Login to rate!
1
2
3
4
def is_pos_activation?
  return yield if block_given?
  session[:pos_activation]
end
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

July 22, 2010, July 22, 2010 07:32, permalink

1 rating. Login to rate!

@Adam: you call the block even if session[:pos_activation] evaluates to false unlike the original code. (Don't know if it matters or has side-effects)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def is_pos_activation?
  return yield if block_given? && session[:pos_activation]
  session[:pos_activation]
end

#OR

def is_pos_activation?
  (block_given? && session[:pos_activation]) ? yield : session[:pos_activation]
end

#OR

def is_pos_activation?
  session[:pos_activation] && (block_given? ? yield : true)
end
Avatar

bob

July 22, 2010, July 22, 2010 16:15, permalink

1 rating. Login to rate!
1
2
3
  def is_pos_activation?
    session[:pos_activation] && (!block_given? || yield)
  end
A8d3f35baafdaea851914b17dae9e1fc

Adam

July 22, 2010, July 22, 2010 20:49, permalink

1 rating. Login to rate!

@Martin - Good point. Refactor requests should be required to come with unit tests. ;)

1
2
3
def is_pos_activation?(&block)
  session[:pos_activation] && (block || Proc.new { true }).call
end
861f311cc4a077c439099d0e5d251e73

Wolfbyte

July 26, 2010, July 26, 2010 07:36, permalink

No rating. Login to rate!

I'm a fan of re-entrancy when blocks are required to be passed in. How about this?

1
2
3
4
def in_pos_activation?(&block)
  return is_pos_activation? { true } unless block_given?
  session[:pos_activation] && yield
end

Your refactoring





Format Copy from initial code

or Cancel