On Tue, Mar 10, 2009 at 3:39 PM, Torm3nt <[email protected]> wrote:

>
> Your turn!!
>
>
>
A favourite of mine is that if, case, etc. are expressions, not statements.

  value =
    if expression
      foo
    else
      bar
    end

or

  value =
    begin
      calculation()
    rescue
      default_value
    end


Here's some real-life code of mine that uses this.  It also uses another
great idiom: multiple assignment.

    @expression, @result, @filename =
      case option
      when "sqrt2"
        two = BigDecimal.new("2")
        ["sqrt(2)", sqrt(two, ACCURACY), "sqrt2"]
      when "1_7"
        one = BigDecimal.new("1.0")
        seven = BigDecimal.new("7.0")
        ["1 / 7", one.div(seven, ACCURACY), "one seventh"]
      when "PI"
        ["PI", PI(ACCURACY), "PI"]
      else
        puts "Usage: ruby cnum.rb { #{OPTIONS.join(' | ')} }"
        exit!
      end

Cheers,
Gavin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to