Skip to content

Instantly share code, notes, and snippets.

@VictorTpo
Last active September 14, 2016 13:09
Show Gist options
  • Save VictorTpo/aaf405c95e2c22da18cbf649dbf9632d to your computer and use it in GitHub Desktop.
Save VictorTpo/aaf405c95e2c22da18cbf649dbf9632d to your computer and use it in GitHub Desktop.
@user = User.new(name: '')
[...]
= text_field_tag :pseudo, nil, value: params[:pseudo] || @user.name
# doesn't work :'(

= text_field_tag :pseudo, nil, value: params[:pseudo].present? ? params[:pseudo] : @user.name.present?
# works, but ugly, x2 character count ! :'(
class String
  def **(other) # 'other' is rubocop's rule for the ** ! ;)
    present? ? self : other
  end
  # be carefull in method name : def |(other) was better, but doesn't work for NilClass : nil | 'foo' > true
end
= text_field_tag :pseudo, nil, value: params[:pseudo] ** @user.name
# works :-D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment