how do I DRY out this ruby if-then statement?

Satchel

I have the following code which seems verbose for what I want it to do:

if @initial_that.present?
  @that = @initial_that
  get_talk_api_response
else
  get_talk_api_response
end

It looks like I could make it DRY, but not sure how.

burnettk

i'm sure there's a computer science name for this refactor, but you're calling get_talk_api_response in both branches, so put it outside the conditional instead:

if @initial_that.present?
  @that = @initial_that
end

get_talk_api_response

and then to make it look more ruby-like, per the hot comment:

@that = @initial_that if @initial_that.present?
get_talk_api_response

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to DRY out this ruby code

From Dev

How do I stay DRY?

From Dev

How do I stay DRY?

From Dev

With a nested if statement, how do i exit out of the highest level if?

From Dev

How do I find out of what size a Float in Ruby is?

From Dev

How do I screen out NaNs when comparing floats in Ruby?

From Dev

How to break out of case statement in Ruby

From Dev

How can I DRY out this F# code? (Fluent Interface)

From Dev

How to dry code with each in ruby?

From Dev

How do I combine an IF statement with an IF(OR) statement?

From Dev

How do I add a Ruby IF statement on the same line as plain text in HAML?

From Dev

How do I stay DRY with Meteor template helpers function composition?

From Dev

How do I stay DRY with Meteor template helpers function composition?

From Dev

How do I refactor modals that operate on different DIVs to be DRY?

From Dev

In Rails 4 how do I add fields to a form in a DRY manner?

From Dev

How do I execute a dry run of a tar compression?

From Dev

Ruby 2.0 How do I uninclude a module out from a module after including it?

From Dev

Ruby 2.0 How do I uninclude a module out from a module after including it?

From Dev

How do I break out of this if?

From Dev

How alias_method work in ruby. How can I use it to achieve DRY?

From Dev

DRY - how to exchange this if-statement into less complex

From Dev

In ruby how do I write a 'do' method in ruby?

From Dev

In ruby how do I write a 'do' method in ruby?

From Dev

I can't figure out how this switch statement is working?

From Dev

I can't figure out how this switch statement is working?

From Dev

I can't figure out how this switch statement is working?

From Dev

How do I use the WITH statement in Access?

From Dev

How do I get this case statement to work?

From Dev

How do I negate a regex statement?

Related Related

  1. 1

    How to DRY out this ruby code

  2. 2

    How do I stay DRY?

  3. 3

    How do I stay DRY?

  4. 4

    With a nested if statement, how do i exit out of the highest level if?

  5. 5

    How do I find out of what size a Float in Ruby is?

  6. 6

    How do I screen out NaNs when comparing floats in Ruby?

  7. 7

    How to break out of case statement in Ruby

  8. 8

    How can I DRY out this F# code? (Fluent Interface)

  9. 9

    How to dry code with each in ruby?

  10. 10

    How do I combine an IF statement with an IF(OR) statement?

  11. 11

    How do I add a Ruby IF statement on the same line as plain text in HAML?

  12. 12

    How do I stay DRY with Meteor template helpers function composition?

  13. 13

    How do I stay DRY with Meteor template helpers function composition?

  14. 14

    How do I refactor modals that operate on different DIVs to be DRY?

  15. 15

    In Rails 4 how do I add fields to a form in a DRY manner?

  16. 16

    How do I execute a dry run of a tar compression?

  17. 17

    Ruby 2.0 How do I uninclude a module out from a module after including it?

  18. 18

    Ruby 2.0 How do I uninclude a module out from a module after including it?

  19. 19

    How do I break out of this if?

  20. 20

    How alias_method work in ruby. How can I use it to achieve DRY?

  21. 21

    DRY - how to exchange this if-statement into less complex

  22. 22

    In ruby how do I write a 'do' method in ruby?

  23. 23

    In ruby how do I write a 'do' method in ruby?

  24. 24

    I can't figure out how this switch statement is working?

  25. 25

    I can't figure out how this switch statement is working?

  26. 26

    I can't figure out how this switch statement is working?

  27. 27

    How do I use the WITH statement in Access?

  28. 28

    How do I get this case statement to work?

  29. 29

    How do I negate a regex statement?

HotTag

Archive