0

I am trying to figure out the best way to use multiple parameters in Ruby. The problem is that I am using two types of parameters: Event and User. Sometimes the method needs one User:

postAccept.replaceVariables(sally)

Sometimes it needs two Users:

deleteGuest.replaceVariables(sally, tom)

Sometimes it needs an Event and two Users:

addUserToEvent.replaceVariables(mEvent, tom, sally)

And last, sometimes it needs one Event and one User:

addnonbookable.replaceVariables(event1, carl)

Here is what I am doing currently in regard to the method:

def replaceVariables(*event)

    if event.size <=2

        if event[0].include?('entryId')
            event1 = event[0]
            my = event[1]
        else
            my = event[0]
            their = event[1]
        end
    else
        event1 = event[0]
        my = event[1]   
        their = event[2]
    end
            ...

The problem is, I can't figure out a way to differentiate between the user and the event. In the example above, I tried to determine if the object has a specific key, or value, but I get NoMethodError.

Can someone tell me what I'm doing wrong, or show me a way to keep things dynamic and flexible?

4
  • 2
    This is really bad style. You should write your methods in a way that they accept a small and rather static number of arguments. You should rather implement multiple methods for the different call patterns, normalize the values and then call another method which implements the actual logic if required. Commented Dec 20, 2012 at 20:02
  • Why does the method sometimes need different types of objects? Commented Dec 20, 2012 at 20:02
  • The reason that I am doing it this way is because it is the simpler way to do it. The method is replacing a number of different environment variables. Currently the method is 220 lines of code. If I break it into 2 methods, that will double. Commented Dec 20, 2012 at 20:07
  • 2
    Whoa ... 1. That's way too many lines. 2. The linecount won't double, you just need one big routine and a couple of 1-line alternate front-ends. Or 3. just take a hash in the first place: f :user1 => tom, :event1 => whatever Commented Dec 20, 2012 at 20:08

2 Answers 2

4
def replace_variables(*args)
  events = args.select{|a| a.is_a? Event}
  users = args.select{|a| a.is_a? User}
  #now you have 2 arrays and can manipulate them
  #...
end

Tin Man suggested implementation in 1 line:

def replace_variables(*args)
  events, users = args.group_by{ |p| p.class }.values_at(Event, User)
  #now you have 2 arrays and can manipulate them
  #...
end
Sign up to request clarification or add additional context in comments.

2 Comments

a.is_a? Event is probably better since that allows subclasses of Event to still be treated as an Event.
The two lines of this method can be replaced with events, users = params.group_by{ |p| p.class }.values_at(Event, User)
1

Check if the first argument is an event. If it is, then it is the only event, and the rest are users, and you can shift out the event. Otherwise, all of them are users.

def replaceVariables(*users)
  event = nil
  if users.first.is_a? Event
    event = users.shift
  end
  # ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.