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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:09:05+00:00 2026-05-12T11:09:05+00:00

ActiveSupport offers the nice method to_sentence . Thus, require ‘active_support’ [1,2,3].to_sentence # gives 1,

  • 0

ActiveSupport offers the nice method to_sentence. Thus,

require 'active_support'
[1,2,3].to_sentence  # gives "1, 2, and 3"
[1,2,3].to_sentence(:last_word_connector => ' and ')  # gives "1, 2 and 3"

it’s good that you can change the last word connector, because I prefer not to have the extra comma. but it takes so much extra text: 44 characters instead of 11!

the question: what’s the most ruby-like way to change the default value of :last_word_connector to ' and '?

  • 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-12T11:09:06+00:00Added an answer on May 12, 2026 at 11:09 am

    As an answer to how to override a method in general, a post here gives a nice way of doing it. It doesn’t suffer from the same problems as the alias technique, as there isn’t a leftover “old” method.

    Here how you could use that technique with your original problem (tested with ruby 1.9)

    class Array
      old_to_sentence = instance_method(:to_sentence)
      define_method(:to_sentence) { |options = {}|
    
        options[:last_word_connector] ||= " and "
        old_to_sentence.bind(self).call(options)
      }
    end
    

    You might also want read up on UnboundMethod if the above code is confusing. Note that old_to_sentence goes out of scope after the end statement, so it isn’t a problem for future uses of Array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use time.ago using standalone ActiveSupport, required thus: ruby require 'active_support/core_ext/date_time/calculations' If
in the rails source : https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb the following can be seen @load_hooks = Hash.new
I found the ActiveSupport inflector class. Where can I find the code that initializes
I can't seem to make the Pending module in ActiveSupport::Testing work. test/unit/pending.rb contains: require
irb(main):001:0> require 'active_support' => true irb(main):002:0> require 'active_support/inflector/inflections' => true irb(main):003:0> ActiveSupport::Inflector.pluralize('test') => test
When I try to do: $ rails generate c2dm_migrations I Get: /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require': no
I install plugin open_id_authentication and have this error: /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `require': no such file to
I get the following error: /srv/www/cyprus-weather.net/cyprus-weather/vendor/cache/ruby/1.9.1/gems/activesupport- 3.1.0/lib/active_support/dependencies.rb:306:in `rescue in depend_on': No such file to
actionmailer actionpack activeresource activesupport I have a basic scaffolded CRUD app that uses ActiveRecord
When running rails generate I get the following error: /usr/local/rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in require': no such file

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.