r/ruby • • Jul 18 '12

Default separator - .join ghetto

http://ruby-blog.pl/2012/07/18/default-separator---join-ghetto/
2 Upvotes

10 comments sorted by

9

u/TaiGat Jul 18 '12

Whole post could be replaced with this link...

-2

u/jrochkind Jul 18 '12

not exactly. That api doc does not make it at all clear that changing the global $, value will change the behavior of String#join, or even of the first example given in the api doc -- you have to notice and realize the significance of the method signature join(sep=$,), which clearly many people miss, even in mature widely used products like ActiveRecord.

Personally, I think the root of the problem is the existence of the Perl-esque $,, I think it's probably unfortunate that it exists.

12

u/tenderlove Pun BDFL Jul 18 '12

People should not be surprised that if they modify global variables, bad things happen. This isn't "overlooking", this is common sense. Let me rephrase the problem from the OP: "modifying a global variable will globally modify the behavior of the program". It does not seem surprising.

Don't forget to also "protect yourself" from modifications of $/, $, $;, $>, $=, etc.

1

u/jrochkind Jul 18 '12

tenderlove, so do you think Rails should not have fixed this bug, instead you just should have said "Well, don't change the global variable, don't expect Rails to actually work if you do"?

I agree that it's kind of crazy to have to 'protect yourself' from modifications of every Ruby perl-style global behavior variable. But very widely used software does not, then you're basically saying "If you modify the value of any of these globals, you can't count on any gem you use actually working." Either the developers of gems need to 'protect', or the users of gems need to never touch any of those globals.

I tend to to think "Never touch any of those globals, it's a mistake that they exist in the first place."

6

u/tenderlove Pun BDFL Jul 18 '12

tenderlove, so do you think Rails should not have fixed this bug, instead you just should have said "Well, don't change the global variable, don't expect Rails to actually work if you do"?

Yes, but that isn't the point of this debate. If you change global variables, it effects your program globally. Globally includes third party software.

-1

u/jrochkind Jul 18 '12

Indeed. The point is that it effects third-party software in generally unpredictable and undocumented ways.

In this case, it just does something weird to your logs, that at worst will make some other tool tryign to parse your logs confused.

It is easy to imagine other cases where it could cause much more severe and obvious problems in the third party code -- that nobody would have predicted, that are not documented, and that are caused because someone writing the third party code did not consider that $, might change, and the person changing $, had no way to know what it might effect in third party code.

Personally, I still think the answer is "Never change $,, and it's unfortunate that it exists at all"

6

u/TaiGat Jul 18 '12

I don't understand how this is not clear unless one does not know how global variables and default method arguments work. Consider the following:

$foo = "abc"
def bar(arg = $foo)
  puts arg
end

bar # prints "abc"
$foo = "def"
bar # prints "def"

Sorry if it seemed like I undermined the post itself - I just think that as far as reddit goes, such basic content should be posted to /r/learnruby instead of /r/ruby :)

7

u/TheDeadSerious Jul 18 '12

Can we quit using the term Ghetto already, please? Vastly inappropriate. If you want to play the blame-game, use tarpit or something.

1

u/postmodern Jul 19 '12 edited Jul 19 '12

In Ruby 1.8 Array#join used to use $/ as the separator. This was changed to $, in Ruby 1.9. You shouldn't rely on the default value of the separator argument in Array#join for 1.8/1.9 compatibility.

Also, stop abusing the word Ghetto.

-2

u/SeattleTomy Jul 18 '12

Don't ever rely on the default separator. Your code will be more clear and less breakable if you document your intent in the code:

['a','b','c'].join(' ')
"This is a string".split(' ')