Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
? and ! are used in method names, but apparently cannot be used in variable names?
foo! = 2
=> SyntaxError: (irb):1: syntax error, unexpected '='
What is the reason?
? and ! are ruby operators, so they are not allowed in variable names. Otherwise, how will Ruby evaluate something like if (v!=2) (expression that checks whether a variable v is not equal to 2) or something like v?1:0 (expression that will return 1 if v is truthy and 0 if its falsy)
UPDATE
Another plausible reason is that Ruby treats names ending with ? and ! as methods
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I capture the text value and store it in a variable. Now I want to use the specific characters from the variable. How can I do it in Ruby?
var = "ABC-DEF-XYZ"
I want to use "DEF" value from the variable and want to skip the rest.
You can capture the 'y' value from your string using one of these 2 methods:
Regex
y = input_value.gsub(/^\w\w\w-(\w\w\w)-\w\w\w$/, "\\1")
Split
y = input_value.split('-')[1]
Once you have the 'y' value from your input, you can compare it to the table list.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was reading something else posted by a user on here explaining how yield statements work in Ruby. Part of the code he was using was :
print_list( [1,2,3], 23 ) { |n| "<#{n}>"}
what do the < > mean inside the string? It's such a simple question but I haven't been able to find out the answer to it.
In a string literal neither < nor > have any implied meaning - although such might have meaning in the output or use of the resulting string.
Only escape sequences and # (in interpolated literals) have intrinsic meaning.
These characters are just a part of string.
And any character which lies inside #{ } will be evaluated, which is also referred to Interpolation
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why doesn't this statement work?
1>2 ? puts "true" : puts "false"
Here, I find that most ruby operators are like C with () parentheses having high priority. This code In C
1 > 2 ? printf("true") : printf("false")
executes successfully. Why is ruby code not working?
The error indicates that ternary operator has lower priority than method argument. Ruby parses around the first instance of puts method up to:
puts "true"
and looks if there is another argument, which should be preceded by a comma if there is any. But you have a colon continuing:
: puts "false"
which cases a syntax error.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a Ruby narray automatically generated, which has this form: [x,y], where x and y are integers.
I want to transform [x,y] into this type of string:
"p\.x\.y"
I haven't succeeded in transforming it using a regular expression.
Regex is used to match patterns, not to generate strings.
To accomplish what you want, just use the splat operator with sprintf:
array = [1,2]
puts sprintf("p\\.%d\\.%d", *array)
This will output "p\.1\.2"
Alternative way:
x = [1,2]
puts "p\\.#{x[0]}\\.#{x[1]}" #=> p\.1\2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find a regex that does the following. Let's say I have a string in this form
wordcount = "THE:12 IT:3 TO:3".
which is a word and its frequency. I need a regex that can find for example THe, followed by :, followed by a number.
If you want all matches use the scan method:
mystring.scan(/\w+:\d+/)
Bonus if you are planning to make a hash:
Hash[mystring.scan(/(\w+):(\d+)/)]
# or, if you prefer to not use regexp:
Hash[x.split.map{|y| y.split(':')}]
You can do as below :
s = "THE:12 IT:3 TO:3"
p s.scan(/\w+:\d+/)
# >> ["THE:12", "IT:3", "TO:3"]