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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:21:03+00:00 2026-05-14T22:21:03+00:00

Class TShirt def size(suggested_size) if suggested_size == nil size = please choose a size

  • 0
Class TShirt

def size(suggested_size)
  if suggested_size == nil
   size = "please choose a size"
  else
   size = suggested_size
  end
end
end 

tshirt = TShirt.new
tshirt.size("M")

== "M"

tshirt = TShirt.new
tshirt.size(nil)

== "please choose a size"

What is a better way to have optional objects in a method? Procs?

  • 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-14T22:21:04+00:00Added an answer on May 14, 2026 at 10:21 pm

    Default values may be what you’re looking for:

    def size(suggested_size="please choose a size") 
    

    You can find some more information about default values over at wikibooks. They also go over variable length argument lists and the option of passing in a hash of options to the method, both of which you can see in a lot of Rails code…

    Variable Argument List

    File vendor/rails/activerecord/lib/active_record/base.rb, line 607:

    def find(*args)
      options = args.extract_options!
      validate_find_options(options)
      set_readonly_option!(options)
    
      case args.first
        when :first then find_initial(options)
        when :last  then find_last(options)
        when :all   then find_every(options)
        else             find_from_ids(args, options)
      end
    end
    

    Options Hash

    File vendor/rails/activerecord/lib/active_record/base.rb, line 1361:

    def human_attribute_name(attribute_key_name, options = {})
      defaults = self_and_descendants_from_active_record.map do |klass|
        "#{klass.name.underscore}.#{attribute_key_name}""#{klass.name.underscore}.#{attribute_key_name}"
      end
      defaults << options[:default] if options[:default]
      defaults.flatten!
      defaults << attribute_key_name.humanize
      options[:count] ||= 1
      I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes]))
    end
    

    Object Attributes

    If you are looking to have optional attributes in an object, you can write a getter and setter method in your class:

    Class TShirt
      def size=(new_size)
        @size = new_size
      end
    
      def size
        @size ||= "please choose a size"
      end
    end
    

    Then you could just call use tshirt.size=”xl” and tshirt.size on an instance of your TShirt class.

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

Sidebar

Related Questions

class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
class Tree def initialize*d;@d,=d;end def to_s;@l||@r?,>:@d;end def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end def insert d alias
class Product < ActiveRecord::Base set_table_name 'produce' end module ActiveRecord class Base def self.set_table_name name
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
I have a website with these menus <li><a href=#>Man</a> <ul class=sub_menu> <li><a href=mcaps>Caps</a></li> <li><a
class MyClass<T> where T: new() { public Test() { var t = new T();
class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what
class ITestType(object): Sample interface type __metaclass__ = ABCMeta @abstractmethod def requiredCall(self): return class TestType1(object):
I have a url that looks like this: http://localhost/store/mens/category/t-shirts/item/a-t-shirt I have a class called

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.