if statements and case statements questions

Discussion in 'Ruby' started by John Crichton, Jul 10, 2010.

  1. I am fairly new to Ruby and programming and had a couple questions about
    if/else and case statements. I was wondering if there were benefits to
    using case statements instead of if/elsif/else type statements. Are
    case statements faster?

    If I am reading in a file line by line and doing something similar to

    lines = File.open("file.csv")
    FasterCSV.parse(lines) do |row|
    if ( lines =~ /^blah/)

    puts "blahblahblah"
    some_method
    end

    if ( lines =~ /^name/ && row[1] =="123")
    puts "ping pong abc"
    another_method
    end
    if ( lines =~ /^address/ && row[3] =="XYZ")
    puts "abcdefghijklmnopqrstuvwxyz"
    method3
    end
    end

    Does writing it with a case statement like so benefit me?
    lines = File.open("file.csv")
    FasterCSV.parse(lines) do |row|
    case
    when ( lines =~ /^blah/)
    puts "blahblahblah"
    some_method

    when ( lines =~ /^name/ && row[1] =="123")
    puts "ping pong abc"
    another_method

    when ( lines =~ /^address/ && row[1] =="XYZ")
    puts "abcdefghijklmnopqrstuvwxyz"
    method3
    end
    end

    While writing this I couldn't figure out how to write my case to be "if
    lines begins with"
    case "lines =~"
    when /^blah/
    puts "blahblahblah"
    some_method

    Lastly is there a "better" way to write an if statement that is
    conditional on a bunch of things like
    if row[0] == john && row[7] == abc123 && row[8] != nil &&
    somehashtable.has_key?(row2)

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    John Crichton, Jul 10, 2010
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    Case statements are not faster. Both Case and if statements are constant
    time operations so there is no speed benefit of one over the other. It
    really just comes down to your preference in any given situation. Depending
    on the code one or the other can lead to cleaner and easier to understand
    code.

    Oh and cool name btw :)

    On Sat, Jul 10, 2010 at 12:30 AM, John Crichton <>wrote:

    > I am fairly new to Ruby and programming and had a couple questions about
    > if/else and case statements. I was wondering if there were benefits to
    > using case statements instead of if/elsif/else type statements. Are
    > case statements faster?
    >
    > If I am reading in a file line by line and doing something similar to
    >
    > lines = File.open("file.csv")
    > FasterCSV.parse(lines) do |row|
    > if ( lines =~ /^blah/)
    >
    > puts "blahblahblah"
    > some_method
    > end
    >
    > if ( lines =~ /^name/ && row[1] =="123")
    > puts "ping pong abc"
    > another_method
    > end
    > if ( lines =~ /^address/ && row[3] =="XYZ")
    > puts "abcdefghijklmnopqrstuvwxyz"
    > method3
    > end
    > end
    >
    > Does writing it with a case statement like so benefit me?
    > lines = File.open("file.csv")
    > FasterCSV.parse(lines) do |row|
    > case
    > when ( lines =~ /^blah/)
    > puts "blahblahblah"
    > some_method
    >
    > when ( lines =~ /^name/ && row[1] =="123")
    > puts "ping pong abc"
    > another_method
    >
    > when ( lines =~ /^address/ && row[1] =="XYZ")
    > puts "abcdefghijklmnopqrstuvwxyz"
    > method3
    > end
    > end
    >
    > While writing this I couldn't figure out how to write my case to be "if
    > lines begins with"
    > case "lines =~"
    > when /^blah/
    > puts "blahblahblah"
    > some_method
    >
    > Lastly is there a "better" way to write an if statement that is
    > conditional on a bunch of things like
    > if row[0] == john && row[7] == abc123 && row[8] != nil &&
    > somehashtable.has_key?(row2)
    >
    > Thanks
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Zundra Daniel, Jul 10, 2010
    #2
    1. Advertising

  3. Ruby switches expand to the === operator, for example:

    case string
    when "" then puts "Empty"
    when /^a/ then puts "Starts with a"
    end

    is equivalent to:

    if string === ""
    puts "Empty"
    elsif string === /^a/
    puts "Contains a"
    end

    Your first way of doing things, with the three if statements, you should
    change the last two to elsifs.
    --
    Posted via http://www.ruby-forum.com/.
     
    Kenneth    , Jul 10, 2010
    #3
  4. Also since you are not just comparing lines but also row numbers, you
    can't use

    case lines
    when ...
    when ...
    end

    I guess you should stick with the if elsif ... end
    --
    Posted via http://www.ruby-forum.com/.
     
    Kenneth    , Jul 10, 2010
    #4
  5. On 10.07.2010 06:30, John Crichton wrote:
    > I am fairly new to Ruby and programming and had a couple questions about
    > if/else and case statements. I was wondering if there were benefits to
    > using case statements instead of if/elsif/else type statements. Are
    > case statements faster?
    >
    > If I am reading in a file line by line and doing something similar to
    >
    > lines = File.open("file.csv")
    > FasterCSV.parse(lines) do |row|
    > if ( lines =~ /^blah/)
    >
    > puts "blahblahblah"
    > some_method
    > end
    >
    > if ( lines =~ /^name/&& row[1] =="123")
    > puts "ping pong abc"
    > another_method
    > end
    > if ( lines =~ /^address/&& row[3] =="XYZ")
    > puts "abcdefghijklmnopqrstuvwxyz"
    > method3
    > end
    > end
    >
    > Does writing it with a case statement like so benefit me?
    > lines = File.open("file.csv")
    > FasterCSV.parse(lines) do |row|
    > case
    > when ( lines =~ /^blah/)
    > puts "blahblahblah"
    > some_method
    >
    > when ( lines =~ /^name/&& row[1] =="123")
    > puts "ping pong abc"
    > another_method
    >
    > when ( lines =~ /^address/&& row[1] =="XYZ")
    > puts "abcdefghijklmnopqrstuvwxyz"
    > method3
    > end
    > end


    I personally prefer the case because then it is immediately clear that
    there is a set of alternatives that belong together. The difference to
    if elsif else end isn't too big though.

    > While writing this I couldn't figure out how to write my case to be "if
    > lines begins with"
    > case "lines =~"
    > when /^blah/
    > puts "blahblahblah"
    > some_method


    I think that has been answered already: since you have more conditions
    there is no easy alternative to the case form where the condition is in
    when clause. If you need only a regexp match as only criterion then you
    can do

    case line
    when /^blah/
    puts "This is a blah line: #{line}"
    when /\.$/
    puts "This line ends with a dot."
    end

    > Lastly is there a "better" way to write an if statement that is
    > conditional on a bunch of things like
    > if row[0] == john&& row[7] == abc123&& row[8] != nil&&
    > somehashtable.has_key?(row2)


    Well, you can encapsulate your condition in something else and use that.
    Example:

    BLAH_LINE = lambda {|line| /^blah/ =~ line && line.length > 87}
    class <<BLAH_LINE
    alias :=== :[]
    end

    case line
    when BLAH_LINE
    puts "This is it."
    ....
    end

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jul 10, 2010
    #5
  6. Kenneth     wrote:
    > Ruby switches expand to the === operator, for example:
    >
    > case string
    > when "" then puts "Empty"
    > when /^a/ then puts "Starts with a"
    > end
    >
    > is equivalent to:
    >
    > if string === ""
    > puts "Empty"
    > elsif string === /^a/
    > puts "Contains a"
    > end


    Actually the arguments are the other way round:

    if "" === string
    puts "Empty"
    elsif /^a/ === string
    puts "Starts with a"
    end

    Another example:

    case arg
    when String; puts "it's a string"
    when Numeric; puts "it's a number"
    end

    The first expands to String === arg, which is functionally equivalent to
    arg.is_a?(String)
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jul 10, 2010
    #6
  7. John Crichton, Jul 12, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Brewer

    RE: Case sensitive and ludicrous statements

    Robert Brewer, Dec 8, 2003, in forum: Python
    Replies:
    6
    Views:
    327
    Mark Jackson
    Dec 11, 2003
  2. Replies:
    5
    Views:
    644
    David T. Ashley
    Jan 15, 2007
  3. traxfern
    Replies:
    0
    Views:
    1,302
    traxfern
    Nov 26, 2011
  4. Farrel Lifson

    Case statements and ERB

    Farrel Lifson, May 10, 2005, in forum: Ruby
    Replies:
    1
    Views:
    133
    Jamis Buck
    May 10, 2005
  5. Eric Will

    barenames and case statements

    Eric Will, Apr 7, 2009, in forum: Ruby
    Replies:
    6
    Views:
    186
    The Higgs bozo
    Apr 7, 2009
Loading...

Share This Page