Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to use the reactive programming concept in ruby, I have create two block of code:
1 Imperative
a = 5, b = 2
c = a + b
print c #=> 7
a = 2
print c #=> 7
2 Declarative
a := 5, b := 2
c := a + b
print c #=> 7
a := 2
print c #=> 4
However second example doesn't work for me and give the below error:
d.rb:1: syntax error, unexpected '=', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a := 5, b := 2
^
Please anyone help me to find out the error in the code. Any suggestion will be highly appreciated.
I know the second one is pseudo code but one thing surprise me that top score person make it off topic? The second code can also be executed using Reactive Library and top score programmer don't aware about it.
The := is not valid Ruby.
The error message is because symbols are represented by leading colons so :example is a symbol (compare to "example" which is a string).
So encountering : Ruby expects a valid beginning character for a symbol, which would be any of...
#$_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Middle characters can be...
_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
And the last character can be...
!_=?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
So = is never a valid symbol character.
The article you reference is showing pseudo-code not actual Ruby.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a very simple equation in vbscript/asp. I get error on minus operator. I even had to add 0 in start of equation because I got same error at first one too.
y = 0 - (0.0114 * (x^2)) − (0.2396 * x) + 112.57
Microsoft VBScript compilation error '800a0408'
Invalid character
y = 0-(0.0114 * (x^2))-(0.2396 * x)+112.57
----------------------------^
The character between the x^2)) and the (0.2396 does not look like the character between the 0 and the (0.0114. It is likely an en dash, not a minus/hyphen. Fix that. Do not use Word or other word processor for creating code; the smart replacement of dashes and quotes will cause problems with code. Use a text editor like Notepad or Notepad++ instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hash#fetch returns the value in a hash for the given key.
It also accepts an optional block:
h = { "a" => 100, "b" => 200 }
h.fetch("a") #=> 100
h.fetch("z", "go fish") #=> "go fish"
h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
One would assume that a block will help us work on the value associated with given the key. However, the block seems to yield the key instead of the value (refer to the third call of fetch above).
What is the use of such implementation? I don't see any point in key being passed to the block as it is already known; it is the value that one is interested in, as is evident by call to fetch.
UPDATE: This is invalid question, I misread the documentation. My apologies
The block is used for dealing with missing values - the value is not yielded because there isn't one.
You're supposed to use the key to return a suitable value (or raise an exception if appropriate)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
So say I have three strings. I am trying to check if the letters in two appear EXACTLY ONCE in "doopdedoo", and if the letters in three appear an unlimited amount of times.
one = "doopdedoo"
two = "dp"
three = "o"
if one.{|a| a.chars.all? {|c| "#{three}".include?(c)}} && one.{|a| a.chars.once? {|c| "#{two}".include?(c)}}
I have used the above to test for the presence of an unlimited amount of o's. How to test for a limited amount of d's and p's?
Edit:
Sorry but I need to clarify. My expected output would be nothing for this case.
[]
Because doopdeedoo contains more than one instance of d or p.
It does contain many o's, so that's fine.
I also added the &&... part to the method above. I realize there is no 'once' method but if there is something like that I'd like to use it.
You can use the String#count method like this:
test_string = "foopaad"
must_appear_once = ['d', 'p']
must_appear = ['o']
must_appear_once.all? {|c| test_string.count(c) == 1} \
and must_appear.all? {|c| test_string.count(c) > 0}
This ensures that 'd' and 'p' each appear exacly once and that 'o' appears in the string (no matter how often).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Take a string like "4 / 10 + 5 x 3"
how would you parse the string in Ruby and evaluate the math?
Note: Hopefully, the solution doesn't involve regex if possible
You can use eval to evaluate a string as Ruby code.
eval(string [, binding [, filename [,lineno]]])
Evaluates the Ruby expression(s) in string. If binding is given, which
must be a Binding object, the evaluation is performed in its context.
If the optional filename and lineno parameters are present, they will
be used when reporting syntax errors.
In your case
string = '4 / 10 + 5 * 3'
eval(string)
Keep in mind that the string you posted is not valid Ruby code. In fact, x is invalid, the correct operator is *.
str = "4 / 10 + 5 x 3"
eval(str.tr("x","*"))
If you want to keep the x you can translate it using tr.
eval can run anything, so don't try this with user input.
str = "4 / 10 + 5 x 3"
result = eval(str)
Your current code will throw a Syntax error since you're trying to multiply using the character x.
You should use * operator.
After that it's simple by using eval(). But normally this should be refrained from.
result = eval("4 / 10 + 5 * 3")
For math parsing I would look into this gem, haven't tried it myself but seems better than raw eval.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've go a string made in this way.
"AABBCCDD....." grouped by 4 with variable lenght.
I need a method that swap that 2 by two the chars in this string
def swap2_by_2( string )
???
end
If the input is AABBCCDD the output will be BBAADDCC
Thanks, i'm very noob in ruby.
Edit: my mistake, a more comprhensive example may be.. Input: ABCDEFGH -> CDABGHEF
It is not clear what the OP is trying to do, but if it is to flip the first and the second characters with the third and fourth characters for every four characters, then the example that the OP showed is highly misleading and inappropriate (It should have been "ABCD..." instead of "AABB..."). In that case, a solution would be:
string.gsub(/(..)(..)/, '\2\1')
Thinking about your question, an interpreting the "ABCDEF", I am sure, that you are looking for pack / unpack in Ruby: I found a good page here How to change bit order in Ruby
And here are two a non-regexp versions:
p 'AABBCCDD'.chars
.each_slice(2)
.each_slice(2)
.map(&:reverse)
.join
#=> "BBAADDCC"
# or
'AABBCCDD'.chars
.each_slice(4)
.map{|x| x.rotate(2)}
.join