str = ‘foo’
=> “foo”
str.each_char do |c|
c = ‘x’
end
=> “foo”
WHY does str == ‘foo’ still?? i would expect it to be ‘xxx’! this makes
no
sense, and is QUITE annoying… i end up having to do things like
this:
… i CAN’T do things like that for a string because it dsn’t seem to do
that, but for an array i’d have to:
ary = [‘f’, ‘o’, ‘o’]
=> [“f”, “o”, “o”]
ary.each_index do |i|
ary[i] = ‘x’
end
=> [“x”, “x”, “x”]
GAH!!! least surprise?? least surprise my FOOT!!! even if there was a
*.each! method to do that i would be fine, that would be ok, none of
this
'get the index and then do what you need to do and THEN set the stupid
block variable… GRRRRRR!!!
The wise man said: “Never argue with an idiot. They bring you down to
their
level and beat you with experience.”
Other than the fact Linux has a cool name, could someone explain why I
should use Linux over BSD?
No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux
On Tue, Aug 9, 2011 at 3:05 PM, Darryl L. Pierce [email protected]
wrote:
WHY does str == ‘foo’ still??
Because each_char is not destructive to the original string’s value. It
moves through the string as an array of characters and then passes each
one by value to the block supplied. So while you might be able to
locally alter the value for that character, you don’t have access to the
original string itself in the block.
so is there a way to do that? easily without jumping through hoops of
fire??
Sometimes it’s your expectation that’s wrong and not the library you’re
using.
The wise man said: “Never argue with an idiot. They bring you down to
their
level and beat you with experience.”
Other than the fact Linux has a cool name, could someone explain why I
should use Linux over BSD?
No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux
sense, and is QUITE annoying… i end up having to do things like this:
There are two reasons.
c is a variable. When you say c = 'x', you are updating the
object
that the variable c is pointing to, not invoking some sort of setter on
the
string you are iterating over (which is what you are doing in the
array
example when you invoke the #[]= method).
Even if you were able to actually change the string, ie using c.replace 'x' rather than c = x, it wouldn’t matter, because the char that c
is
pointing to is a completely different string than str.
I don’t know what you’re trying to do, so I can’t suggest a better
solution,
but there is a way to do what you are asking using String#gsub!
str = “abc”
str.gsub!(/./) { |char| ‘x’ }
str # => “xxx”
GAH!!! least surprise?? least surprise my FOOT!!! even if there was a
*.each! method to do that i would be fine, that would be ok, none of this
'get the index and then do what you need to do and THEN set the stupid
block variable… GRRRRRR!!!
On Wed, Aug 10, 2011 at 03:52:59AM +0900, serialhex wrote:
ok, so code:
str = ‘foo’
=> “foo”
str.each_char do |c|
c = ‘x’
end
=> “foo”
WHY does str == ‘foo’ still??
Because each_char is not destructive to the original string’s value. It
moves through the string as an array of characters and then passes each
one by value to the block supplied. So while you might be able to
locally alter the value for that character, you don’t have access to the
original string itself in the block.
i would expect it to be ‘xxx’! this makes no
sense, and is QUITE annoying…
If you look at the source code for each_char you’ll see that its return
value is the original string itself. Which is why you see “foo”
returned.
Sometimes it’s your expectation that’s wrong and not the library you’re
using.
On Wed, Aug 10, 2011 at 03:52:59AM +0900, serialhex wrote:
ok, so code:
str = ‘foo’
=> “foo”
str.each_char do |c|
c = ‘x’
end
=> “foo”
Try this:
str.gsub!(/\w/, 'x')
WHY does str == ‘foo’ still?? i would expect it to be ‘xxx’!
The common way to handle things in Ruby is to not change the receiver
of a message. To do otherwise is surprising.
… i CAN’T do things like that for a string because it dsn’t seem to do
that, but for an array i’d have to:
ary = [‘f’, ‘o’, ‘o’]
=> [“f”, “o”, “o”]
ary.each_index do |i|
ary[i] = ‘x’
end
=> [“x”, “x”, “x”]
. . . or use a map:
ary.map! {|element| element.sub /\w/, 'x' }
GAH!!! least surprise?? least surprise my FOOT!!! even if there was a
*.each! method to do that i would be fine, that would be ok, none of this
'get the index and then do what you need to do and THEN set the stupid
block variable… GRRRRRR!!!
You’re operating on c, not on str. It would be more surprising if when
operating on c it changed str, in my opinion.
The each method is an iterator – not an editor. If you want to map an
operation onto an array, you should use the map method. If you want to
do substitutions on characters in a string, use a substitution method.
What could be less surprising than using the right tool for the job?
I think you’re surprised because you expect one tool to do everything.
I would be pretty surprised if someone could make such a tool that
wasn’t
bad at everything it did.
ok, many responses, all very good and reasonable, but i guess my point
is
kinda missed so i’ll explain deeper…
here is an awesome kludge of a program i wrote last night:
yes it’s ugly, and yes it’s horrible, but it’s not meant to be used for
anything more than what it was used for (which is quickly converting
python
to ruby, and give kinda workable results most of the time, and since
it’s
simply single file programs that i was going to edit & fix later, i’m
not
worried as to how ugly the translator is, or the translation, it just
did
90% of my work for me)
look at the strip_python_loads method, that is the best way i can come
up
with at the moment. yes i know there are better ways (to write this and
the
entire program) but what are they? if i wanted to edit the contents of
an
array while iterating through it how do it do it besides that? like i
said,
a method such as each! which will allow you to modify the receiver would
be
nice…
hex
p.s. oh, and if you want to fork & edit that gist to make it prettier &
more
useful feel free to do so!
The wise man said: “Never argue with an idiot. They bring you down to
their
level and beat you with experience.”
Other than the fact Linux has a cool name, could someone explain why I
should use Linux over BSD?
No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux
On Wed, Aug 10, 2011 at 04:38:36AM +0900, serialhex wrote:
worried as to how ugly the translator is, or the translation, it just did
90% of my work for me)
look at the strip_python_loads method, that is the best way i can come up
with at the moment. yes i know there are better ways (to write this and the
entire program) but what are they? if i wanted to edit the contents of an
array while iterating through it how do it do it besides that? like i said,
a method such as each! which will allow you to modify the receiver would be
nice…
def strip_python_loads
@file_lines.map! do |line|
line = '' if line.match(/\s*from[ A-Za-z0-9\_.]*import\s*\w*/])
line
end
end
Warning: I have not tested this code. I may have made a boneheaded
error. There may also be a better way to do this that would have
occurred to me if I was not multitasking.
The wise man said: “Never argue with an idiot. They bring you down to
their
level and beat you with experience.”
Other than the fact Linux has a cool name, could someone explain why I
should use Linux over BSD?
No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux
c represents a temporary variable with a copy of the char in it. This is
always the case. Use the map and join methods if you want to construct a
new string in that manner, and then set the variable pointing to the old
string to the new string.
text = <<PYTHON
from foo import bar
batz
PYTHON
def strip_python_loads(input)
input.map do |line|
unless line =~ /\s*from[ A-Za-z0-9\_.]*import\s*\w*/
line
end
end
#same here, #map! works destructively
end
puts strip_python_loads(text.split("\n")).join("\n")
# or even
puts strip_python_loads(text.lines).join("\n")
Regards,
Florian
On Aug 9, 2011, at 9:38 PM, serialhex wrote:
worried as to how ugly the translator is, or the translation, it just did
p.s. oh, and if you want to fork & edit that gist to make it prettier & more
should use Linux over BSD?
No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux
Editing the contents of an array while you’re iterating it is generally
considered bad programming practice… Because if you delete things,
indexing becomes unpredictable. Using copies of arrays is usually a
better practice generally.
If you want to replace or strip things from strings, use those methods.
Don’t recreate the wheel. I suggest you look into the string class’s
methods fairly deeply.