How do conditional operators work in ruby [closed] - ruby

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.

Related

Ruby: how to interpolate in a string? [closed]

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 10 months ago.
Improve this question
Is a simple question about how to print an interpolation on a string in ruby
My method is this:
def hello(parameter)
puts "hello + #{parameter}"
end
puts hello(name)
but irb, show me these message:
NameError: undefined local variable or method `name' for main:Object
It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.
Thank you, it's my first post ejje.
The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").

What is the syntax error in this short ruby block? [closed]

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 5 years ago.
Improve this question
What is the syntax error in this short ruby block?
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this (1,2,3)
end
end
do_this (4,5,6)
I get errors on the fourth and seventh lines, where "do_this" is called.
The error is: 'unexpected ")", expecting "." or...' [...]
Remove spaces between method name and parentheses.
You need to avoid using spaces between the method call and the arguments in parenthesis:
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this(1,2,3)
end
end
do_this(4,5,6)

Calling proc as a hash key in Ruby [closed]

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 8 years ago.
Improve this question
Refers to - bizzare way of calling Procs?
#BroiSatse thanks a lot for the answer. One additional moment to clarify - what if one needs to pass extra arguments (added to the code as param_1 and _2):
def callbacks(param_1, param_2, procs)
procs[:var_1].call(param_1)
puts "Proceed"
procs[:var_2].call(param_2)
end
callbacks(arg_1, arg_2, :var_1 => Proc.new {block_1},
:var_2 => Proc.new {block_2})
What goes first? i.e. what will be passed first to execute the def callbacks- arguments (arg_1, arg_2) in place of params (param_1, param_2) or procs (:var_1, :var_2)? This is important to know to properly code the params line - def callbacks(param_1, param_2, procs).
Thanks in advance for any help.
I can't see how you think this will possibly make a difference here, but the arguments are evaluated from left to right and in YARV are pushed onto the stack in that order. Obviously, though, they are all passed before the called method starts executing.

exception in if statement [closed]

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 wrote a searcher function in ruby for a class that iterates on a list of people (named #personList).
It faces an exception.
My Code‌ :
def search (nCode)
for x in #personList
if x.nCode == nCode
x.to_s
end
end
I wrote the same code using each, it also faced the same exception.
What's wrong with my code ?
(I'm new to Ruby! and I couldn't solve that)
This if is delimited by a pair of newlines:
x.to_s if x.nCode == nCode
This if needs an explicit end:
if x.nCode == nCode
x.to_s
end
You have opened three blocks (def, for..in, if) in your first sample, but only closed two. Your indentation indicates the if is the one you didn't close. Thus, you have a syntax error. Otherwise, both samples are identical in functionality.
Ruby if requires an end to make it complete. If you use end for your exception code, it will work.
if x.nCode == nCode
x.to_s
end
For second case, that worked or you, it is actually completes the condition and the execution in one line. That's why it didn't require any end, and it worked for you.
You'll find more detail about if from here.

Why can't punctuation (? and !) be used in Ruby variable names? [closed]

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

Resources