Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner assignment conditional is a pretty bad example... #39

Closed
jtmarmon opened this issue Nov 24, 2015 · 8 comments
Closed

Inner assignment conditional is a pretty bad example... #39

jtmarmon opened this issue Nov 24, 2015 · 8 comments

Comments

@jtmarmon
Copy link
Contributor

Why would you ever do

retval = if vals.any?
           do_something(vals)
         else
           do_something_different
         end

over

retval = vals.any? ? do_something(vals) : do_something_different

I think the inner assignment conditional only makes sense when more logic needs to be performed inside the conditions

@franzejr
Copy link
Owner

Nice, @jtmarmon, we can put that in one line exactly. My point here was to show that we can also use

retval = if vals.any?
           do_something(vals)
         else
           do_something_different
         end

rather than:

if vals.any?
          retval =  do_something(vals)
 else
           retval = do_something_different
end

But, for sure it's best your solution in one line. What about we put the one line solution and also this solution? What do you think?

@jtmarmon
Copy link
Contributor Author

Well I think you could best refine the page to - rather than focusing on specific examples - showing the multitude of ways you can be "lispy" in ruby (as ruby is quite lisp inspired).

Examples:

retval = if vals.any?
           do_something(vals)
         else
           do_something_different
         end
retval = case my_val
         when "foo"
           2
         when "bar"
           3
         else
           5
         end
retval = vals.any? ? do_something(vals) : do_something_different
retval = foo unless bar

etc. generally performing assignment on expressions that use first class operators. this was just a few off the top of my head. if you like the idea for the revision i'd be happy to PR it

@franzejr
Copy link
Owner

I liked the idea! I think the multitude of ways is nice! And we can have the one line and also those examples you did. Looking forward for your PR.

Thanks! 👍

@jtmarmon
Copy link
Contributor Author

on it. any ideas for the title of the article?

@franzejr
Copy link
Owner

My idea at the beginning it was "inner assignment", because I would like to remove the assignment inside my conditional. Since, we are still working with assignments, conditionals and we're trying to do the same thing... Why not keep the title or something like "Conditional Assignments" ?

@jtmarmon
Copy link
Contributor Author

yeah i like it

@franzejr
Copy link
Owner

👍 looking forward for your PR!

@jtmarmon
Copy link
Contributor Author

#40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants