SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry default value in methods

    hi,

    I have problems with default values. I don't know whether I'm stupid or doc is not good (you're not supposed to answer ) but I spend a lot of time for trivial things.

    According to Wiki RailsBestPractices, it's a good idea to add a default value like that:

    PHP Code:
    def self.custom_findid_to_find )
        ...
    end 
    When I try to add a default value, I get an error:
    [edit: the query is ok without = 0 default value ]
    PHP Code:
    def self.find_query(categorytypecity_idprice_min 0price_max)
        
    find(:all,
        
    sql conditions coming here...)
      
    end 
    parse error, unexpected ')', expecting '='

    Did I miss something?

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only the last parameters can have default values. How would you call it otherwise?

    find_querycat, :type, :city_id, ????, 100)

    ??

    So ruby is expecting the last parameter to have a default value too. That's why it needs a =, instead of ):

    Code:
    def self.find_query(category, type, city_id, price_min = 0, price_max = 100000) 
                                                                          ^ =, not )

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Fenrir2, it works but I find it weird as it's not mandatory, just a best practice.
    Plus it's not used when the value is empty, it seems to work only with null values.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's there so you can omit these arguments if you call your procedure. It is by no means a best practice, there are special cases where it is handy. Only use default values if it makes sense.

    It is there so you can call your procedure this way:

    Code:
    Foo.find_query(a_category, a_type, a_city_id)
    And don't worry about the min/max price.

    You cannot do this for a parameter in the middle because it makes no sense to omit it if you have parameters after it.

    Another use case is Rails' find. You can use:

    Foo.find(23) => find row with id=23

    And:

    Code:
    Foo.find(:first, :conditions => 'a = "b"')
    So you can use the same method in multiple ways.

  5. #5
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the tips Fenrir2


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •