Why is there no `elsunless` statement in Ruby? [closed] - ruby

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.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Ruby provides unless and elsif statements. It seems natural to assume that there would be a similar elsunless statement, but there is not. Is there a specific reason for this?
To illustrate, this statement would allow for code like this.
unless broken
# do something
elsunless done
# do something else
end
I'm aware that this code can be rewritten to use if and elsif, but in some cases using unless is clearer.

The logic behind if / else statements usually is:
Having one exception:
if exception_a
# do exception stuff
else
# do standard stuff
end
unless exception_a
# do standard stuff
else
# do exception stuff
end
Adding unless in this case can be very useful, as you can switch around your code. What I also love about unless is that you can solely do your standard stuff while checking for an exception. (the else block can be left out)
Having multiple exceptions:
Here comes the tricky part:
if exception_a
# do exception stuff a
elsif exception_b
# do exception stuff b
else
# do standard stuff
end
unless exception_a
# do standard stuff
elsunless exception_b
# do ???
else
# do exception stuff
end
Besides being totally unreadable, I couldn't find a logical meaning to the elsunless block: What code would you put in there? I still have no idea if that would be some exception stuff or standard code.
Maybe you can explain further what code you would use in such a block.

Ruby already provides if, else, elsif, and unless, so is there really a need to for elsunless? It looks like a hulking mammoth in a code. I think Matz doesn't see a reason to add the statement into a the ruby syntax.
Additionally, some of ruby coders investigate a ruby coding standard that excludes unless statement usage, which was inherited from Perl.
As for me, I would completely remove the unless keyword from the language.
Have a look at the styling guide

Related

Check if ruby variable has changed [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
I have a function that checks a software buffer for data, however if that data has not changed it is still added and sent.
So I end up getting data sent multiple times.
I'm trying to check to see if my variable has changed, and if it has not I want to prevent it from sending that data again.
Here is what I've tried.
$buffer = fldigi.add_to_buffer(msg)
if $buffer
fldigi.send_buffer()
else
puts "Debug(buffer): Data has not changed"
end
You might try wrapping $buffer in an object and then following the Observer Pattern. Any changes to $buffer must go through the wrapper object's methods. Observers can then subscribe to the change notification events emitted by the $buffer wrapper and act accordingly such as fldigi.send_buffer().
There's no way to know whether a variable has changed without tracing the execution of the program. Variables aren't objects in Ruby, you can't tell them to do something, you can't ask them questions, you can't pass them as arguments, you can't return them as values, you can't assign them to variables, you can't call methods on them.
There's only two things you can do with a variable: dereference it and assign to it.
Assignment is the only way to change the variable, therefore you need to trace assignments to the variable to figure out whether or not it was changed. Note, however, that the standard Ruby TracePoint API does not offer the necessary information (probably because variables might get optimized away by the compiler).
It is probably going to be much easier to just check whether some object has changed rather than whether some variable has changed.
prev_val = nil
loop do
new_val = fldigi.add_to_buffer(msg)
if new_val == prev_val
puts "Debug(buffer): Data has not changed"
else
fldigi.send_buffer()
end
prev_val = new_val
end

reverse method is always false? [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 8 years ago.
Improve this question
I don't know why this isn't working properly, it always gives me false.
More importantly is there an easy way to see line-by-line execution of the code? Something like an easy trace that would help me troubleshoot this (I tried irb, but couldn't get it to work with multiline example like this one).
puts 'enter a word and I will tell you if its a palindrome or not: '
word = gets.chomp
backwards = word.reverse
if word == backwards
puts "yes, it is a palindrome!"
else
puts "no, #{word} is not a palindrome."
end
EDIT:
Sorry, I had a typo in my word. This is working fine. I feel like an idiot. Which brings me to my second question above... a good way to trace code execution in irb or elsewhere... is that possible?
The pry gem is an excellent debugging tool.
https://github.com/pry/pry
Install it by typing on the command line...
gem install pry
Then in your code at the point you want to halt execution, enter this line
require 'pry'; binding.pry
You can then inspect all variables, even change them if you want, and type 'exit' when you're ready to resume.
If you do require 'pry' at the beginning of your program, you can just do binding.pry where you need to halt... in that case you don't need the two-command expression I used above.

Improving the readability of PL/SQL for identifying the LOOPs and Individual blocks [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
I am relatively new to PL/SQL (Oracle). I am able to complete my assignment successfully. Having followed Modular programming, I divided my program into small PL/SQL blocks. Also I have nested BEGIN-END in my code. (To handle exceptions)
When my code base started to grow, I could see many nested blocks inside, and I am unable to identify the corresponding END for every BEGIN block .(When the blocks are bigger). The same case with the nested FOR LOOPs too. I agree there's no curly brackets in PL/SQL.
I improved the indentation of my code to the best of my ability, and to some extent the code is readable now. But still, if anyone else wanted to read my code, I have a feeling that my code may not be easy to traverse.
Do you guys provide some suggestions to solve my issue?
<<outer_loop>>
LOOP
<<<block1>>
BEGIN
i := i + 1;
j := 0;
<<inner_loop>>
LOOP
-- Your Statements
EXIT inner_loop WHEN (j > 5);
EXIT outer_loop WHEN ((i * j) > 15);
END LOOP inner_loop;
END block1;
END LOOP outer_loop;
Try using LABELS (embedded between angled brackets). This should help you!
Your can look for the label names, for where the block/loop starts or ends!
Actually, this kind of Label can be used for GOTO too
But dont over use it, as it also confuses you :)
Good Luck!
I can think of the following solutions. I guess a combination of them should help.
1) Turn your inner PL/SQL Blocks into procedures. This will shorten the blocks.
2) Use a PL/SQL Editor (SQL Developer, PL/SQL Developer, TOAD, SQL Navigator are all quite popular among the community.) to view the code. Each editor has its own solution such as indicating matching begins and ends with brackets, code folding etc...

Bug in Ruby RPG (skips line in case..when) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am currently developing a text-based RPG in Ruby, however there are some bugs in the programming code. The code is long, so I'll provide a link to the Source Code HERE.
The problem occurs at line 113. Whenever I enter "y" for case randEvent1 it accepts it and does the following lines in the when statement but when I put in "yes", it skips all of those lines goes to the line:
puts "Do you want to go to a Tavern next?"
puts "Or maybe you want to go to the forest?"
My question is, why does it skip the lines when I put in "yes" for randEvent1 even though I put that it will execute the following the when statement if randEvent1 == "y" || "yes"?
This problem also occurs when I put in "no" for the case RandEvent1.
Could it be possible it is a problem with doing || in a case..when statement? Is the syntax different for the or operator in case..when than in if statements?
Case statements do not accept || like ifs do. The proper syntax for a case having two or more possible inputs is ,. So, instead of doing case 'y' || 'yes', it should be case 'y', 'yes'.

Why does Eclipse complain about "Feature envy" smell in my code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Eclipse (RedRails) complain about "Feature envy" in the following code:
if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m
content_text = input_text[($1.size + $2.size)..-1] # warning in $1
header = YAML.load($1)
#content = content_text.strip()
#title = header["title"]
end
My understanding is that I safe to ignore this warning. But I am wandering why this warning is generated. I cannot understand how I can extract method for $1.size and $1.
Reek is telling you that, because you are adding two properties of the same class, the calculation should actually belong in String. When adding string lengths this is nonsense of course, but in your case the code can be simplified by using $& (the complete matched string):
input_text[$&.size..-1]

Resources