Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:19:27+00:00 2026-05-27T15:19:27+00:00

I have three values (foo,bar,bad) and based on which one I pass into a

  • 0

I have three values (foo,bar,bad) and based on which one I pass into a function I want to use the other two.

For example calling self.method(foo) would result in something like this with foo being undefined.

def method
  self.foo = 180 - self.bar - self.bad
end

I can do it with a simple if-elsif-else setup, but is there a better (more idiomatic) way?

Update for clarity:

Here’s what one suggestion might look like in production:

def solve_angles(missing)
  angles = 180 - [ A, B, C ].reject { |e| e == missing }.inject(:+)
end

called via @triangle.solve_angles(self.C) or even ‘@triangle.solve_angles(“C”) would be possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:19:28+00:00Added an answer on May 27, 2026 at 3:19 pm

    You don’t need to specify which angle you’re solving for; it’s implicit in the problem’s definition. If you start with something like this (anything resembling error handling elided):

    class Triangle
      def initialize h
        h.keys.each { |key| instance_variable_set "@#{key}".to_sym, h[key] }
      end
    
      def to_s
        "a=#{@a}, b=#{@b}, c=#{@c}"
      end
    
      def solve
        angle = instance_variables.inject(180) { |v, a| v -= instance_variable_get(a) }
        [:@a, :@b, :@c].each {|s| instance_variable_set(s, angle) unless instance_variable_defined? s }
        self
      end
    end
    

    Then:

    pry(main)> t = Triangle.new :a => 20, :c => 30
    => a=20, b=, c=30
    pry(main)> t.solve
    => a=20, b=130, c=30
    pry(main)> 
    

    You could also return/indicate which angle was actually solved for, if necessary.

    This doesn’t actually avoid an if statement, which was your specific question. It eliminates the need to explicitly spell each one of them out, which I took as the intent of the question.

    If you truly need to “solve for”, you could do add:

    def solve_for sym
      solve
      instance_variable_get("@#{sym}".to_sym)
    end
    

    Technically, you could solve only after determining the value isn’t set, but meh.

    > t = Triangle.new :a => 20, :c => 30
    => a=20, b=, c=30
    > t.solve_for :b
    => 130
    > t
    => a=20, b=130, c=30
    > t = Triangle.new :a => 20, :c => 30
    => a=20, b=, c=30
    > t.solve_for :a
    => 20
    > t
    => a=20, b=130, c=30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of possible values: @a = qw(foo bar baz); How do
I have three values I need to align in a dropdown box. How can
I have three radio buttons with same name and different values.When I click the
I have a query, say Select foo from bar Foo is a string field,
Suppose I have the following C++ class: class Foo { double bar(double sth); };
I have the following nodes in xsl: <foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> <bar>4</bar> <bar>5</bar> <bar>6</bar>
Let's say we have the following html: <ul id=portfolio-list class=clearfix span-24> <li class=2012#foo bar
I have many JSP files with EL expressions of the form ${foo.bar.baz.phleem1} , ${foo.bar.baz.phleem2}
I've always passed arguments to a function like so: setValue('foo','#bar') function setValue(val,ele){ $(ele).val(val); };
Until like one hour ago, I was convinced that in python Foo ().bar ()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.