clement
clement

Reputation: 71

delete last value of a splitted string

I want to look into a hash but the last value should be there :

params send to controller

{
 "user"=>{"email"=>"[email protected],
 [email protected]"}

method

def set_users
    @users = params[:user][:email]
    logger.debug "#{@users.split(",").each { |e| puts e }}"
end

logs

[email protected]
 [email protected]
["[email protected]", " [email protected]"]

The point is that #each takes a value (["[email protected]", " [email protected]"]) which is not in the hash. How can I apply #each to the whole hash except on this value

Upvotes: 0

Views: 96

Answers (3)

dddd1919
dddd1919

Reputation: 888

Rewrite your method:

def set_users
  @users = params[:user][:email].split(',')
  @users.pop
end

Then you can use @users without last one and get last one's info by the return value from set_users method.

Upvotes: 0

apneadiving
apneadiving

Reputation: 115541

I'd do:

@users.split(",")[0..-2]

Namely, take everything but the last.

So:

logger.debug @users.split(",")[0..-2].join(', ')

Seems you just need:

logger.debug @users

Upvotes: 2

Star Chow
Star Chow

Reputation: 1

You should use the String#gsub method like this:

def set_users
    email = params[:user][:email]
    params[:user][:email] = email.gsub(/,.*/i, '')
end

Upvotes: 0

Related Questions