开发者

Is there a more functional way, than using custom case statements, to write this in Ruby?

Imagine the following code:

class SimpleLetter
  def values
    ("a" .. "z").to_a
  end

  def ===(other)
    values.include?(other)
  end
end

class Vowel < SimpleLetter
  def values
    ["a","e","i","o","u"]
  end
end

class Consonant < SimpleLetter
  def values
    super - Vowel.new.values
  end
end

objects = ("a" .. "f").to_a + (1 .. 5).to_a

objects.each do |letter|
  case letter
    when Vowel.new
      puts "#{letter} it's a vowel"
    when Consonant.new
      puts "#{letter} it's a consonant"
    else
      puts "#{letter} it's something else"
  end
end

I could have chosen any other classes instead, I am just using them as an example. I like Scala's match and extractors a lot, and I figured this could be a nice way of writing the same thing in Ruby. Is there a better way of writing the above 开发者_如何学Cwithout having to instantiate new objects just so I could call their === method?

Just to avoid unnecessary posts, yes, I know I could do this:

case letter
  when ["a","e","i","o","u"].include?(letter)
    # ...
end


You don't need classes for the characters. Set them as arrays, and use the splat operator in the case statement.

SimpleLetter = ("a" .. "z").to_a
Vowel        = %w[a e i o u]
Consonant    = SimpleLetter - Vowel

(("a" .. "f").to_a + (1 .. 5).to_a).each do |letter|
  case letter
    when *Vowel
      puts "#{letter} it's a vowel"
    when *Consonant
      puts "#{letter} it's a consonant"
    else
      puts "#{letter} it's something else"
  end
end


=== works on blocks, too:

Letters = ('a'..'z').to_a
Vowels = ['a','e','i','o','u']
Consonants = Letters - Vowels

Vowel = lambda { |x| Vowels.include? x }
Consonant = lambda { |x| Consonants.include? x }

objects = ("a" .. "f").to_a + (1 .. 5).to_a

objects.each do |object|
  case object
    when Vowel
      puts "#{object} is a vowel."
    when Consonant
      puts "#{object} is a consonant."
    else
      puts "#{object} is an object."
  end
end


You could use class methods instead of instance methods:

class SimpleLetter
    def self.values
        ("a" .. "z").to_a
    end 

    def self.===(other)
        values.include?(other)
    end 
end

class Vowel < SimpleLetter
    def self.values
        ["a","e","i","o","u"]
    end 
end

class Consonant < SimpleLetter
    def self.values
        super - Vowel.values
    end 
end

objects = ("a" .. "f").to_a + (1 .. 5).to_a

objects.each do |letter|

    case letter
        when Vowel
            puts "#{letter} it's a vowel"
        when Consonant
            puts "#{letter} it's a consonant"
        else
            puts "#{letter} it's something else"
    end 

end


The code you have confused me, because SimpleLetter should be a single letter, rather than the whole alphabet.

I'd be half tempted to do the following, although monkeypatching is a bit risky:

module Voweliness
  def vowel?
    self =~ /[aeiou]/i
  end

  def consonant?
    (self =~ /[a-z]/i and not vowel?)
  end
end

class String
  include Voweliness
end


objects.each do |letter|
  case
    when letter.vowel?
      puts "#{letter} is a vowel"
    when letter.consonant?
      puts "#{letter} is a consonant"
    else
      puts "#{letter} is something else"
  end
end


You already have several good answers (e.g. sawa's), so I'm including a just for fun one without a case statement:

SIMPLE_LETTER = [*"a" .. "z"]
VOWEL        = %w[a e i o u]
CONSONANT    = SIMPLE_LETTER - VOWEL

[*?a..?f,*1..5].each do |letter|
  letter_class = %w(vowel consonant).select { |c| Object.const_get(c.upcase).include? letter}.first
  puts "'#{letter}': #{ letter_class || "something else"}"
end

Output:

'a': vowel
'b': consonant
'c': consonant
'd': consonant
'e': vowel
'f': consonant
'1': something else
'2': something else
'3': something else
'4': something else
'5': something else

The multi-splat and character literals only work in 1.9.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜