Ruby delete method (string manipulation) - ruby

I'm new to Ruby, and have been working my way through Mr Neighborly's Humble Little Ruby Guide. There have been a few typos in the code examples along the way, but I've always managed to work out what's wrong and subsequently fix it - until now!
This is really basic, but I can't get the following example to work on Mac OS X (Snow Leopard):
gone = "Got gone fool!"
puts "Original: " + gone
gone.delete!("o", "r-v")
puts "deleted: " + gone
Output I'm expecting is:
Original: Got gone fool!
deleted: G gne fl!
Output I actually get is:
Original: Got gone fool!
deleted: Got gone fool!
The delete! method doesn't seem to have had any effect.
Can anyone shed any light on what's going wrong here? :-\

The String.delete method (Documented here) treats its arguments as arrays and then deletes characters based upon the intersection of its arrays.
The intersection of 2 arrays is all characters that are common to both arrays. So your original delete of gone.delete!("o", "r-v") would become
gone.delete ['o'] & ['r','s','t','u','v']
There are no characters present in both arrays so the deletion would get an empty array, hence no characters are deleted.

I changed
gone.delete!("o", "r-v")
to
gone.delete!("or-v")
and it works fine.

You get same o/p using some different way like gsub
puts "deleted: " + gone.gsub('o', '')
o/p
deleted: Got gone fool!

Related

How do I best dump a Psych::Nodes::Document back to a YAML string?

This does what I want, but going via to_ruby seems unnecessary:
doc = Psych.parse("foo: 123")
doc.to_ruby.to_yaml
# => "---\nfoo: 123\n"
When I try to do this, I get an error:
DEV 16:49:08 >> Psych.parse("foo: 123").to_yaml
RuntimeError: expected STREAM-START
from /opt/…/lib/ruby/2.5.0/psych/visitors/emitter.rb:42:in `start_mapping'
I get the impression that the input needs to be a stream of some sort, but I don't quite get what incantation I need. Any ideas?
(The problem I'm trying to solve here, by the way (in case you know of a better way) is to fix some YAML that can't be deserialised into Ruby, because it references classes that don't exist. The YAML is quite complex, so I don't want to just search-and-replace in the YAML string. My thinking was that I could use Psych.parse to get a syntax tree, modify that tree, then dump it back into a YAML string.)
Figured out the incantation after finding the higher-level docs at https://ruby-doc.org//stdlib-2.3.0_preview1/libdoc/psych/rdoc/Psych/Nodes.html, though please let me know if there's a better way:
doc = Psych.parse("foo: 123")
stream = Psych::Nodes::Stream.new
stream.children << doc
stream.to_yaml
# => "foo: 123\n"

TypeError: cannot concatenate 'str' and 'pygame.Surface' objects

I am currently teaching myself game programming, and I've started nice and easy with pygame. I went through a tutorial that showed me how to build a simple game, and now that I am finished with it, I am in the process of trying to reorganize the code in a manner that makes sense to me, and also to edit it and add to it.
Part of what I tried to change is that instead of loading one '.png' file for a character, I load a list of them that will be iterated through in a 'move()' function I designed to make the characters look like they are moving. However I keep running into an error and I don't know why. Near the beginning of my code (all I've done is imported necessary modules and initialized pygame and some necessary variables) I tried to do the following code:
badguyimgs = ['badguy.png', 'badguy2.png', 'badguy3.png', 'badguy4.png']
for img in badguyimgs:
badguyimgs.append(pygame.image.load("resources/images/" + img))
badguyimgs.remove(img)
I keep getting the following error:
TypeError: cannot concatenate 'str' and 'pygame.Surface' objects
So far I have tried to initialize a new variable (resource = "resources/images/" + img) and place that at the beginning of the "for" loop and then insert that into the pygame.image.load(). I've also tried using os.path.join("resource/images/" + img). I've tried using the full path name ("c:\\Users\\ . . . \\resources\\images\\" +img). But any time I try to concatenate the pathname with the file name in the list, I get the above error code. I tried looking in the pygame documentation, but didn't see anything that helped in this situation. I've tried googling the error, but get nothing in reference to this. (a lot of issues with people tring to concatenate int types to strings though. . . ) I would appreciate any help anyone could give in pointing out why I am experiencing this, and what could fix it. Thanks.
It looks like what you're doing is appending the pygame.surface object (that you loaded from a png file) to the list while you're iterating through it. You are loading the images successfully. However after your function adds the first image and removes the string, your list looks like this:
badguyimgs = ['badguy2.png', 'badguy3.png', 'badguy4.png', pygame.image]
You are still iterating through the list, so your function starts trying to concatenate the string and the pygame.surface object. I would recommend creating an empty list, and add your loaded images to that list without adding or removing anything from the original. Hope this helped!
Here's an example to go with PlatypusVenom's explanation:
file_names = ['badguy.png', 'badguy2.png', 'badguy3.png', 'badguy4.png']
images = []
for file_name in file_names:
images.append(pygame.image.load("resources/images/" + file_name))
Now the pygame.Surface objects are in images, and the variable names for the lists are less confusing. Another option is to use a list comprehension:
images = [pygame.image.load("resources/images/" + file_name) for file_name in \
("badguy.png", "badguy2.png", "badguy3.png", "badguy4.png")]
This is similar to what you were going for in the code posted. The list of strings will be removed from memory, leaving only pygame.Surface objects in the images list.

Julia doing something strange with assignments

I am trying to learn Julia by repeating some of the easy ProjectEuler problems in Julia. Everything has been really smooth so far, up until I encountered this frustrating problem. I spent some time debugging my code, and here's what I found:
(Hopefully I'm not missing something really stupid here)
function is_abundant(n::Int) #just a function
return prod(map(x->int((x[1]^(x[2]+1)-1)/(x[1]-1)),factor(n))) > 2 * n
end
abundants=[12] #there should be a better way to initialize an Array
for i=13:28120
if is_abundant(i)
push!(abundants,i)
end
end
le=abundants; #The following lines are the problems
ri=abundants;
d=length(abundants)
println(d)
pop!(le)
shift!(ri)
println(le==ri, " ", endof(ri), " ", endof(abundants))
The output I get is:
6964
true 6962 6962
which means that Julia has changed all three sets of le , ri and abundants with each of pop! and shift! commands. I was able to work around this bug/problem by using a dumb extra identity mapping:
le=map(x->x,abundants)
ri=map(x->x,abundants)
Now the output would change to what I initially expected:
6964
false 6963 6964
My question is, if this is not a bug, why is Julia keeping an equivalence relation between le , ri and abundants sets in the first place? Also, can anyone reproduce this behaviour? I am using Julia "Version 0.3.0-rc3+14 (2014-08-13 16:01 UTC)" on Ubuntu 14.04.
le and ri both point to the same list that abundants points to, so this is expected behavior - they are all operating on the same memory. This part of the manual might help you understand. Or possibly the MATLAB differences section, as it is different in MATLAB (but most other languages are like Julia).
For
abundants=[12] #there should be a better way to initialize an Array
how about
abundants = {} # Vector of anything
or
abundants = Int[] # Vector of ints
and instead of your map(x->x,...), you can just use copy.

difference between iterating lines with while and array assignment

I'm writing a perl script that reads a file into an array. I wrote the program on Windows, using Perl 5.16 (it also works on 5.14), and the script failed using a Mac with Perl 5.12.
The part that failed is this: my #array = <$file>. On the Mac, the array came back the correct size (same as number of lines in the file), but every element except the last one was empty. The code worked correctly when I switched to this instead:
my #array;
while(<$file>){
push #array, $_;
}
I'm not sure if it would have made a difference if I switched the line endings to be LF instead of CRLF (Windows style). Though the problem is fixed, it leaves me puzzled. I thought those two code snippets I listed were exactly the same thing. What is the difference in them that produces different results here?
The answer is that the two methods are exactly equivalent, as you suspected. Example:
my $start = tell DATA; #store beginning filehandle position
my #array1 = <DATA>;
seek DATA,$start,0; #reset filehandle position
my #array2;
while(<DATA>){
push #array2,$_;
}
print "List assignment:\n #array1\n";
print "Looping through:\n #array2\n";
__DATA__
1
2
foo
bar
Your previous failure was likely something else. Perhaps some sort of problem with Perl on Mac or Mac's file IO was involved, but more likely it was some other part of your code (by this I mean nothing personal: I would make the same assumption about my own code).

New line or Carraige Return syntax problems

Im pretty new to coding, heres my problem.
Results->Text = "G55 > Y" + System::Convert::ToString(destY);
"Results" is a System.Windows.Forms.Textbox "multiline btw", or so says VS.
That line works fine, but i need a "new line or CR" at the end, so that i can repeat that line with different Literals and a different var in ToString.
For days now ive tried different syntax's ive read about, and i cant get it to take any of them.
Or even a complete different way to input text into Results->Text that would allow for tidy multiline use.
Sidenote: since im using ->Text and System::Convert::ToString in VC, would this code be considered just c++ or .net or CLI? to tighten my searches.
Have you tried System::Environment::NewLine? This will give you CrLf on Windows and whatever is correct for Linux/OS X on those platforms.
Being completely unfamiliar with .NET, I could be completely wrong, but surely adding a + "\n" to the end of your line would do the job? Or failing that, a + "\r\n"?

Resources