In C++, I can use the following code to implement the functions.  char result=0; for(char c:s)    result^=c;   However, when I try to implement this in Ruby, it fails.  result=0 s.chars.each do |c|    result^=c end   How should I XOR each char in a string in Ruby? Thanks very much!
122863
I have a string, lets say "123|ABC|test|12345|FF" and I want to xor the ascii value of each character and print the result in hex.  What is the simplest way?
23377
In C++, I can iterate over an std::string like this:  std::string str = "Hello World!";  for (int i = 0; i &lt; str.length(); ++i) {     std::cout &lt;&lt; str[i] &lt;&lt; std::endl; }   How do I iterate over a string in Python?
7530
In Ruby, given an array in one of the following forms...  [apple, 1, banana, 2] [[apple, 1], [banana, 2]]   ...what is the best way to convert this into a hash in the form of...  {apple =&gt; 1, banana =&gt; 2}
89294
Let's say I have a Ruby class:  class MyClass   def self.property     return "someVal"   end    def self.property=(newVal)     # do something to set "property"     success = true      return success # success is a boolean   end end   If I try and do MyClass.property=x, the return value of the whole statement is always x. It is a convention in a lot of C-based/inspired languages to return a boolean "success" value - is it possible to do this for a setter using the "equals syntax" in Ruby?  Furthermore - if this isn't possible, why not? Is there any conceivable downside to allowing an "equals setter" operation return a value?
39845
Using Java 6, how can I implement a ? It is very easy and possible in Ruby. How can I get similar in Java?
23996
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:  &gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa'   Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated.
610
I've always wondered how to write the "A ? B : C" syntax in a C++ compatible language.  I think it works something like: (Pseudo code)  If A &gt; B    C = A Else    C = B   Will any veteran C++ programmer please help me out?
120154
I want to be able to run unstrusted ruby code. I want to be able to pass variables to said untrusted code that it may use. I also want said code to return a result to me. Here is a conceptual example of what I am thinking  input = "sweet" output = nil Thread.start {    $SAFE = 4    #... untrusted code goes here, it uses the input variable(s)    #to calculate some result that it places in the output variable } #parse the output variable as a string.   Just to clarify, I am basically using the untrusted code as a function. I want to  provide its some inputs, and then allow it to write to the output. That is all I really want, I don't care how it is done, I just want the ability to use untrusted Ruby code as a sort of function. The solution does not have to look anything like the code I wrote above, I am just using it to illustrate what I want.  Now, I can currently think of 3 ways to do this:   Use the $SAFE level construct above. whytheluckystiff has a Sandbox plugin for ruby I could run each function in its own virtual machine, using some sort of os virtualization software like vmware or Xen or something.   I am wondering if anyone has any recommendations for running untrusted ruby code in a functional way? What option would you recomend? How would you go about it? Thanks.
7247
Ruby can add methods to the Number class and other core types to get effects like this:  1.should_equal(1)   But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can't be modified?  Update: Rather than talking about different definitions of monkey patching, I would like to just focus on the example above. I have already concluded that it cannot be done as a few of you have answered. But I would like a more detailed explanation of why it cannot be done, and maybe what feature, if available in Python, would allow this.  To answer some of you: The reason I might want to do this is simply aesthetics/readability.    item.price.should_equal(19.99)   This reads more like English and clearly indicates which is the tested value and which is the expected value, as supposed to:  should_equal(item.price, 19.99)   This concept is what  and some other Ruby frameworks are based on.
44916
What is the best way of doing case-insensitive string comparison in C++ without transforming a string to all uppercase or all lowercase?  Please indicate whether the methods are Unicode-friendly and how portable they are.
28149
• $C_n =$ this number of bit strings  • A binary string with no adjacent 0s is constructed by   Adding “1” to any string w of length $n-1$ satisfying the condition, or Adding “10” to any string v of length $n-2$ satisfying the condition   I still don't find that intuitive at all. Why adding up $C_{n-1} + C_{n-2}$ results in $C_n$? I have tried my best and asked my friends for help, but they find the formula unintuitive as well. Could anyone here come up with a clear explanation? Thanks!
82601
I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?
46146
I often use the execv() function in C++, but if some of the arguments are in C++ strings, it annoys me that I cannot do this: const char *args[4];  args[0] = &quot;/usr/bin/whatever&quot;; args[1] = filename.c_str(); args[2] = someparameter.c_str(); args[3] = 0;  execv(args[0], args);  This doesn't compile because execv() takes char *const argv[] which is not compatible with const char *, so I have to copy my std::strings to character arrays using strdup(), which is a pain. Does anyone know the reason for this?
72830
Are the string literals we use inside functions automatic variables? Or are they allocated in heap which we have to free manually?  I've a situation like the code shown below wherein I'm assigning a string literal to a private field of the class (marked as ONE in the code) and retrieving it much later in my program and using it (marked as TWO). Am I assigning a variable in the stack to a field in ONE? Can the code be referencing to a dangling pointer which in this case worked because the program was small enough?  I've compiled and ran it, it worked fine but I'm having a strange crash in my actual program where I'm assigning string literals to fields of the class like this and I suspect the case I mentioned above.  #include &lt;iostream&gt;  using namespace std;  class MemoryLeak { private:     char *s; public:     MemoryLeak() {}      void store()     {         s = "Storing a string"; // ONE     }      char *retrieve()     {         return s;     } };  int main() {     MemoryLeak *obj = new MemoryLeak();     obj-&gt;store();     cout &lt;&lt; obj-&gt;retrieve() &lt;&lt; endl; // TWO     delete obj;     return 0; }   Should I be declaring the variable "s" as a char array instead of a pointer? I'm planning to use std::string, but I'm just curious about this.  Any pointers or help is, as always, much appreciated :) Thanks.
81943
How does one parse an integer to string(char* || char[]) in C? Is there an equivalent to the Integer.parseInt(String) method from Java in C?
96075
Here is a quick question, how to use tex to print this ' symbol? An example is shown below:  data 'a list = Nil | Cons (e, 'a list)   I tried to use ' or `, but they all looks bad.. I am sorry but I am not a native English speaker, so I don't even know the name of '..   Could anyone help me on this simple problem? thank you
7953
Can a string be used as array index in C?  Ex: String      Corresponding value "ONE"       1 "TWO"       2 "FIVE"      5 "TEN"       10  When a string in the above list is passed to the function, the function must return the corresponding value indicated above. Can this be achieved by declaring a constant array with string as index  int *x; x["ONE"]  = 1; x["TWO"]  = 2; x["FIVE"] = 5; x["TEN"]  = 5;  return x["string received by the function"];   The above logic does not work as expected; is there a workaround to implement the above logic in order to have a string-indexed array?