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 5952443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:40:58+00:00 2026-05-22T17:40:58+00:00

Imagine the following code: class SimpleLetter def values (a .. z).to_a end def ===(other)

  • 0

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 without 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
  • 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-22T17:40:59+00:00Added an answer on May 22, 2026 at 5:40 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following code: MyService myService = new MyService(); myService.Start(); myService.DoStuff(); The MyService class
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
Imagine the following REBOL code: foo: context [bar: 3] I now have a context
Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new
I have the following code in my header file: class Factovisors { public: Factovisors(std::istream&
I got my code like the following: class TimeManager { public: virtual ~TimeManager(); };
Imagine I have the following code that runs as a background processor for an
Imagine the following A type T has a field Company. When executing the following
Imagine the following tables: create table boxes( id int, name text, ...); create table

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.