This question already has answers here:
Blocks and yields in Ruby
(10 answers)
Closed 8 years ago.
Yield seems to be neither object nor method. What is it? How does it access the block that is passed as an argument to the method?
yield is a keyword, just like while or end or return.
"How" it accesses the block is not really interesting, no more than "how" a return keyword delivers a value to the calling context, or "how" an end keyword closes a block - unless you want to dive into development of Ruby interpreter itself. The important bit for a Ruby programmer is just that that's what it does.
Related
This question already has answers here:
What is the usefulness of `enable_shared_from_this`?
(6 answers)
Closed 5 years ago.
What is the point for a class T to inherit from std::enable_shared_from_this<T> ? I can't seem to figure out why you wouldn't just create a std::shared_ptr<this> ?
Cppreference has a good example on why.
If you want to return a std::shared_ptr of this while *this is already owned by a std::shared_ptr and you don't return shared_from_this() but return a new std::shared_ptr<T>(this) instead then you will end up with 2 shared pointers that don't know they're both owning the same object and thus the use_count() will be wrong which will cause a double delete, which is undefined behavior.
This question already has an answer here:
What does self mean in Ruby? [duplicate]
(1 answer)
Closed 6 years ago.
Currently I am reading The Ruby Programming Language and it has mentioning of self in many places and I am not able to understand it's exact use. Is its behaviour similar to this pointer in C++.What all difference those two have?(I am not asking about the pointer dereferencing or any C++ specific things, in general) And when do we use self.something in our ruby code?
self is a variable that points to the object whose scope the current code is in. You would use self.something when calling a class method on that object.
For instance, if you had the following method:
class Foo
def self.bar
puts "Class method!"
end
end
You would call the bar method by calling Foo.bar.
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
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
Sometimes I see code like
class Thing
def self.add_em(a,b)
a+b
end
end
and sometimes I see
class Thing
def self.add_em(a,b)
#a=a
#b=b
#a+#b
end
end
When/Why should I use the # instance variables instead of just using the parameters as passed?
I believe that one reason is if you want to use those variables in any other method then instance variables will be available and local, parameter based variables will not. However I frequently see # variables being used even though the variables are not being used in any other method.
So I see the pattern of
#a=a
#b=b
at the start of method for all parameters passed in being used a lot but I'm not clear exactly why if they are just used on that method. Is it just a convention in case they are used in other methods?
As you correctly realized, it does not make sense to define instance variables unless they are used in another method. If instance variables are used but are not called in any other method, then that code is probably not written by a good programmer.
But note that sometimes, method definitions are not obvious at first look. For example, if there is
class Thing
attr_reader :a
end
then there actually is a method that uses #a.
I'd say that they did it because they had plans to reference the arguments as instance variables. If not they failed the YAGNI (you aint gonna need it principle). If they changed their minds half way through (which has been known to happen...), they they forgot to tidy up.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
When do you use the “this” keyword?
In OOP sometimes we can write this.PropertyName = VALUE and sometimes we can skip this. and just write PropertyName = VALUE.
My questions:
Should we try always to use this.?
Does using / writing this have any effect on application performance or does it just make the code a little bit clearer?
There shouldn't be any difference in performance. Its purely a style decision.