How to modify the value of a variable whose name is given - ruby

I wish to have a method that takes in a string, and then updates a variable with the name of that string. This is an example of my attempt:
#other_class = OtherClass.new()
def change_variable(variable_string)
self.#other_class.send.variable_string += 1
end
I am getting the error:
syntax error, unexpected tIVAR
with the pointer just before 'send' in the method above. Does anyone have any suggestions to make this work?

You probably want instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set
The syntax using your variables is I think:
other_var = ('#' + variable_string).to_sym
#other_class.instance_variable_set( other_var, #other_class.instance_variable_get( other_var ) + 1 )

The immediate syntatic error is that you're using self and # wrongly.
Either one is fine but not in conjunction with each other.
So in your case self.other_class.send... would be fine but then you cant declare it as #.
As would # be but then you cant do self.
These are meant to do different things and these are that
# is an instance variable and so is self but the difference is that using # calls the attribute other_class directly as to where self calls the method other_class.
So # is both a getter and setter in one so you can do
#other_class = my_milk_man as to where
self.other_class -> self.other_class (as getter),
self.other_class = my_milk_man -> self.other_class= (as setter).

Related

How to make parameters flexible in ruby?

Im pretty new to ruby . I have a method lets say -" method_X " with parameters (client_name, client_dob).
def method_X client_name, client_dob
(BODY OF THE METHOD)
end
Now I want to introduce a third parameter let's say "client_age". I want my method_X to have a flexibility in taking the parameters.I'm getting an error to mandatorily enter client_name if I forget. I should have flexibility to not mandatorily enter all the three parameters as input. How can I achieve this? Thank you in advance!
In Ruby, you can declare parameters as required, default and optional arguments.
required - required parameters need to be passed otherwise it throws an error.
Ex: def method_X(client_name)
In this, you need to send the client_name argument, else it throws an error.
default - default parameters are optional arguments, but you should declare the default value for the given parameter while defining the method. So that you can skip the argument if you want or you can send a new value while calling the method.
Ex: def method_X(client_name="Abc Company")
In this case, if you haven't passed the client_name argument for the method, the default will be Abc Company. You can default to any value you like, say nil, empty string, array etc.
optional - Optional parameters where you need to use the splat(*) operator to declare it. This operator converts any number of arguments into an array, thus you can use it if you don't know how many arguments you will pass. If no arguments, it gives an empty array.
Ex: def method_X(*client_name)

PostgreSQL "ERROR: could not determine data type of parameter" Ruby exec_params

I am trying to execute a query that selects recipes that match a search term from user input stored in the query variable. This is the portion of relevant code:
class DatabasePersistence
def initialize(logger)
#db = if Sinatra::Base.production?
PG.connect(ENV['DATABASE_URL'])
else
PG.connect(dbname: "recipes")
end
#logger = logger
end
def search_recipes(query)
p "Query parameter is:"
p query
p query.class
sql = <<~SQL
SELECT * FROM recipes
WHERE labels ILIKE '%$1::text%'
SQL
results = query(sql, query)
# ... more code
end
def query(statement, *params)
#logger.info "#{statement}: #{params}"
#db.exec_params(statement, params)
end
end
The following error is raised on when this line results = query(sql, query) is executed.
PG::IndeterminateDatatype at /search
ERROR: could not determine data type of parameter $1
Another post suggested adding an explicit type cast which is why I added the type cast. I could be doing it incorrectly. I also tried it like the following:
WHERE labels ILIKE '%text($1)%'
WHERE labels ILIKE '%cast($1 as text)%'
WHERE labels ~ '$1::text'
WHERE labels ~ 'cast($1 as text'
In all of the above cases it returned the same error "could not determine the datatype of parameter. I added some #p method calls to make sure the query variable is referencing a real value for debuggin. I have confirmed that this error occurs when the query references a string object with value oats.
What is causing this error to still occur if I am casting the datatype and it is not nil? Am I passing the parameters incorrectly? Am I casting the parameters incorrectly? Is it possible there is a way to pass datatypes as arguments to the #exec_params method? Is there another way to safely pass parameters to be executed by the instance of the PG.connect class?
Simply:
WHERE labels ILIKE $1::text
I assume labels is a plain character type like text, too.

How to make Ruby Mocha mock only check about one parameter

I want to mock this function:
def self.set_segment_info(segment_info, history_record)
history_record.segment_info = segment_info
end
In my test, I want a mock that only confirms that I called set_segment_info with an expected value. I don't care about what I pass in for history_record.
How would I do this? I tried
SegmentHistoryRecord.expects(:set_segment_info).with(:segment_info => expected_segment_info, :history_record => anything)
But that doesn't work.
I ran into this today and ended up doing something like:
SegmentHistoryRecord.expects(:set_segment_info).with(
expected_segment_info,
anything
)
I find it more readable that the do version and it helped me avoid a rubocop issue with too many parameters.
Here's an implementation where, if your function takes a lot of parameters, it's more convenient to specify a value for just the one you care about, instead of for all of them:
expected_segment_info = # ...
SegmentHistoryRecord.expects(:set_segment_info).with() { |actual_parameters| actual_parameters[:segment_info] == expected_segment_info }
(Where, as in the original question, set_segment_info is the function being mocked, and segment_info is the parameter whose value you want to match. Note that the history_record parameter -- and any others that might be present -- don't need to be included.)
SegmentHistoryRecord.expects(:set_segment_info).with() do |param1, param2|
# change below to your verification for :segment_info
# and leave param2 doing nothing, the expectation will ignore param2
param1 == expected_segment_info
end

What is my method returning here?

I have a question that I've already found the solution to (or perhaps it is just chance), but I'm hoping someone can explain why it works, and what Ruby is doing being the scenes here.
I'm doing something with fixed width output text and ANSI color codes. I don't want the escaped characters to count towards my length, so I wrote a little method for the String class to calculate the length excluding the color codes:
def length_minus_codes
color_codes = [ "\033[30m",
"\033[0m" ,
"\033[31m",
"\033[32m",
"\033[33m",
"\033[34m",
"\033[35m",
"\033[36m",
"\033[37m",
"\033[40m",
"\033[41m",
"\033[42m",
"\033[43m",
"\033[44m",
"\033[45m",
"\033[46m",
"\033[47m",
"\033[1m",
"\033[22m",
"\033[7m",
"\033[27m"]
#Create new variable to strip
stripped_self = self
#loop through color code array
for index in 0 ... color_codes.size
#strip color codes from string
stripped_self.gsub!(color_codes[index],"")
end
#return variance of self to stripped self to
#get length of string not including color codes
return self.length - (self.length - stripped_self.length)
end
end
I thought it was working fine, until I realized that after it was called, the string it was called on had the character codes stripped from it.
I tried a few things, before decided to change this:
stripped_self.gsub!(color_codes[index],"")
To this:
stripped_self = stripped_self.gsub(color_codes[index],"")
Now it is working fine.
What I don't understand is why? I understand the basic concept of in place methods (!) which I was using on the gsub, but it wasn't modifying self, but rather a variable that I set in the method, and second I only want to return the length of the string, not an actual string.
Can anyone explain what is happening here?
When you do
stripped_self = self
you are simply creating a new reference to the self string object, you are not creating a new string. So any in-place modifications (by gsub! in this case) will be reflected on the self object.
If you want to create a new object that is not a reference, you need to duplicate the object:
stripped_self = self.dup
Possibly a simpler solution here is just to use the non-bang version of gsub and save that to a variable. gsub! changes the receiver as bang methods often do, gsub will simply return a modified object safely without effecting the receiver.

Is 'buggy_logger' a reference to the 'status' string in this Ruby example?

In this example, do the nukes get launched because any changes that you make to buggy_logger get applied to the 'status' string - just like using a copy of a reference to an object -> when you make a change to the copy of the reference, the change gets applied to the underlying object -> that change is, in turn, reflected in any other references to the object. So, in other words, buggy_logger is an alias to the 'status' object without specifically using the alias keyword? Is that correct? So, in ruby, you just say
b = a
and then any changes you make to b afterwards are also reflected in a. Or is this only true because we're talking about Strings, which are mutable in Ruby?
# example-4.rb
status = "peace"
buggy_logger = status
print "Status: "
print buggy_logger << "\n" # <- This insertion is the bug.
def launch_nukes?(status)
unless status == 'peace'
return true
else
return false
end
end
print "Nukes Launched: #{launch_nukes?(status)}\n"
# => Status: peace
# => Nukes Launched: true
Yes, it is because strings are objects. Try
buggy_logger = status.dup
If you want a distinct object with the same initial value.
As for your question about alias I suspect you aren't correctly understanding how alias is used in ruby; it's used on methods, not objects, and isn't related to mutability.
Note also that the same semantics would have applied with any class; if status had been an array, a file, or anything else (provided it had mutable state suitable for use as a logger), you would have gotten analogous results.
One warning about dup though. If your object refers to other objects, the copy will also refer to the same objects. It's fine once you start thinking about it the right way, but tricky till then.

Resources