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
Related
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 8 years ago.
Improve this question
I have object initializer in my code, that initializes every field of my object explicitly. But in my case, most of the parameters have sensible defaults and I want to use them.
In Python I usually use combination of keyword arguments or defaults and my __init__ method contains some validation logic so I can use zero configuration principle in object initialization. For example:
class Foo:
"""This class designed to show zero configuration
principle in action"""
def __init__(self, mandatory, optional=None, **kwargs):
self.__field1 = mandatory
self.__field2 = optional or make_default2()
if 'bar' in kwargs:
self.__field3 = kwargs['bar']
else:
self.__field3 = make_default3()
f = Foo('mondatory', bar=Bar())
There is no parameters with default values in Go nor keyword parameters or function overloads. Because of that - it is difficult to write flexible initialization code (I don't care much about performance in such code usually).
I want to find most idiomatic way to write such code in Go. Maybe some combination of runtime type reflection and maps will do the job, what do you think?
Because newly-allocated memory in Go is always zeroed, the idiomatic way is to make explicit use of this fact by:
designing your structs to have have sane zero values
using composite literals
Take a look at the following section of Effective Go:
http://golang.org/doc/effective_go.html#data
For extremely complex cases, a configuration struct (option 3 at http://joneisen.tumblr.com/post/53695478114/golang-and-default-values) is also sometimes used, with a NewConfigStruct() that initializes a configuration instance with defaults. The user generates a default instance, sets the fields they want, then passes it to the New function for the actual struct they are creating.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I recently answered a question on this site (Delete from Array and return deleted elements in Ruby) so well in my opinion that while writing I decided I'd try to submit a pull request for my nifty little method that would make ruby just a bit more awesome I think.
So I have a few questions, the most pressing of which is, Where is the Array class in ruby's source code? I can't find it. If its not there, is it in another repository that I should commit to? Or should I add it somewhere else in some initializer?
Where do new array methods like #uniq and #drop get added? Because I have a new one. #exclude.
To put it one way, #exclude is to #delete as #drop is to #shift. Just as #shift returns what has been removed from the array, so does #delete. #exclude is supposed to be like #drop for #delete. Extremely simple. Seems to work well.
class Array
def exclude(obj)
x = self
x.delete(obj)
x
end
end
I may also add a #pop equivalent for #drop, #popdrop or #backdrop. Am open to suggestions
class Array
def drop(n) #drop could have been implemented like this.
x = self
x.shift(n)
x
end
def popdrop(n)
x = self
x.pop(n)
x
end
end
I'm supposed to be working right now so don't have much time to edit this question. Mods let me know if I need to add more detail to the question -- specifically I just don't know where to put it. I've already forked the repo and am trying to just make a commit on github, which you can do now, but don't know where to put the code, or even if I should actually make a pull request at this point (sandbox?, IRC?, etc).
Ruby is coded in the language C for performance.
https://github.com/ruby/ruby/blob/trunk/array.c
You may be better off creating a gem, or writing a blog about it.
Otherwise you can write your Array deletion method in C. Here is the actual deletion method to help out.
https://github.com/ruby/ruby/blob/trunk/array.c#L2919
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.
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
So I have this line:
if self.company_changed?
And it works fine but this detects if the company has changed on the object. I need to know if the database value has changed and not if the value in memory has changed. So I tried this:
if :company_changed?
This seems to work in debug mode when I only execute the one line. If I let it run, it fails in testing on an infinite loop.
My question is what can be used in ruby to check to see if the column value has actually changed.
I'm pretty sure you're actually talking about ActiveRecord. In which case, you'd need to re-fetch the record to see if the value has changed in the database.
self.class.find(self.id).company != self.company
A general purpose method for this might be something like:
def attr_changed_in_db?(attr)
self.class.find(self.id).attributes[attr] != self.attributes[attr]
end
There is an excellent screencast on this by the great Ryan Bates.