what's the difference between these two snippets of ruby?

Discussion in 'Ruby' started by Mr. Bill, Jan 15, 2011.

  1. Mr. Bill

    Mr. Bill Guest

    Sorry I couldn't help the line wrapping. Thanks in advance.

    parent_path =
    File.expand_path(config['parent'],
    File.dirname(yaml_path)).gsub('${project_dir}',
    PROJECT_ROOT)


    config['parent'].gsub!("${project_dir}", PROJECT_ROOT)
    parent_path = File.expand_path(config['parent'],
    File.dirname(yaml_path))

    --
    Posted via http://www.ruby-forum.com/.
    Mr. Bill, Jan 15, 2011
    #1
    1. Advertising

  2. Mr. Bill

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sat, Jan 15, 2011 at 8:06 AM, Mr. Bill <> wrote:

    > Sorry I couldn't help the line wrapping. Thanks in advance.
    >
    > parent_path =
    > File.expand_path(config['parent'],
    > File.dirname(yaml_path)).gsub('${project_dir}',
    > PROJECT_ROOT)
    >
    >
    > config['parent'].gsub!("${project_dir}", PROJECT_ROOT)
    > parent_path = File.expand_path(config['parent'],
    > File.dirname(yaml_path))
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    The second version modifies config['parent']

    If you want the second version, which is much easier to understand, imo. But
    don't want the side effect of changing the config hash, then use gsub
    without the bang, and assign it to a local variable:

    parent = config['parent'].gsub("${project_dir}", PROJECT_ROOT)
    parent_path = File.expand_path( parent , File.dirname(yaml_path) )
    Josh Cheek, Jan 15, 2011
    #2
    1. Advertising

  3. Mr. Bill

    Mr. Bill Guest

    Got it. Thank you much.


    Josh Cheek wrote in post #975146:
    > On Sat, Jan 15, 2011 at 8:06 AM, Mr. Bill <> wrote:
    >
    >> File.dirname(yaml_path))
    >>
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    > The second version modifies config['parent']
    >
    > If you want the second version, which is much easier to understand, imo.
    > But
    > don't want the side effect of changing the config hash, then use gsub
    > without the bang, and assign it to a local variable:
    >
    > parent = config['parent'].gsub("${project_dir}", PROJECT_ROOT)
    > parent_path = File.expand_path( parent , File.dirname(yaml_path) )


    --
    Posted via http://www.ruby-forum.com/.
    Mr. Bill, Jan 17, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rach
    Replies:
    2
    Views:
    423
    Andy Fish
    Jun 9, 2004
  2. Guybrush Threepwood
    Replies:
    7
    Views:
    487
    Nick Hounsome
    Feb 26, 2004
  3. JohnJSal
    Replies:
    13
    Views:
    429
    John Salerno
    Nov 4, 2006
  4. oo@@oo
    Replies:
    1
    Views:
    289
    Hallvard B Furuseth
    Aug 4, 2006
  5. Manoj
    Replies:
    4
    Views:
    447
    Tomasz Krol
    May 10, 2010
Loading...

Share This Page