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

M

Mr. Bill

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))
 
J

Josh Cheek

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

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))
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) )
 
M

Mr. Bill

Got it. Thank you much.


Josh Cheek wrote in post #975146:
File.dirname(yaml_path))
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) )
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top