.sample on an array is always false in irb [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 7 years ago.
Improve this question
I am testing in irb and everytime I require my file is is always returning false.
class Weather
def stormy
[false, true].sample
end
end
it seems to return false know matter what is in the array?
I am unsure if it is my code or the way I am requiring file in irb.
can anyone help
Thank you

To pick randomly from an Array use sample:
def stormy
[ false, true ].sample
end

The return value of require (or require_relative) indicates whether the file was loaded successfully or was already loaded. It has nothing to do with any methods that may be defined inside the file.
Once the file defining it has been loaded, to call your method (as currently defined) you would do
Weather.new.stormy

Related

Ruby Class error output [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 6 years ago.
Improve this question
can anyone tell me why the last line of my code output nothing? THX
You need to use initialize not init method,
class Mystuff
def initialize
#apple="I am instance"
end
def talk
puts 'I am taking'
end
attr_reader :apple
end
thing = Mystuff.new
thing.talk
puts thing.apple
output
I am taking
I am instance

String replace to method [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 7 years ago.
Improve this question
I need to replace a localized string to localized method.
From:
"social_1.localized()"
To:
"social_1".localized()
What is the best way to do?
May be this:
"social_1.localized()".gsub(".localized()","").localized()
or
my_string, my_method = "social_1.localized()".split('.')
my_method = my_method.gsub!("()",'').to_sym
my_string.send(my_method)
#uri-agassi (see comment) is right. using send this way may be a security risk. especially if it comes from user input (i.e. from the params object). you could think about to whitelist callable methods:
if [:upcase, :downcase, :capitalize].include?(my_method)
my_string.send(my_method)
end
Or at least ask the object, that it knowns the method to call:
my_string.send(my_method) if my_string.respond_to?(my_method)

Ruby detect if a column value 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
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.

Should I use File.write? [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 5 years ago.
Improve this question
I recently run into problems converting a ruby script to .EXE because I had a File.write statement in it. The documentation doesn't mention the write method but when I do a
pp File.methods
it is there. So should I use File.write? In a normal Ruby script the following works
File.write("test.txt", "test")
But is it good practice and why doesn't the documentation mention it?
File.write is in fact IO.write (File is a child of IO) which can be verified by monkey-patching:
class IO
def IO.write
puts "IO's class method write was called."
end
end
File.write # outputs "IO's class method write was called."
It is very well in the documentation.
Thus, I see no reason not to use it.

Ruby 1.9, return array if keys include a particular object [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
Code:
#albums = #genres.each_with_index { |item,key|
if item.keys.include?('Albums')
break
end
}
This should be returning the Albums array (the #genres object is a huge multidimensional JSON response)
I reckon this is what I get for trying to code while being sick... or just simply doing things wrong... either way, any help is much appreciated!
I think you want #detect (or its synonym #find):
#albums = #genres.detect { |item| item.key?('Albums') }['Albums']
EDIT | Also note that you can provide an argument to break just like you can do with return, if you want to break and return a specific value.

Resources