Should I use File.write? [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 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.

Related

Parse code file [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 5 years ago.
Improve this question
nd maybe there is some tool for this? I must extract entire blocks and subblocks like 'it'
I doubt any parser could help to load code blocks into variables. It would require eval, but even with eval it would be extremely hard to collect all the context etc.
It the target is rspec scenarios, I would go with monkeypatching rspec core, prepending your own detectors like:
def before(*args, &block)
MyCollector.collect_block(block)
super(*args, &block)
end
You can parse it with
https://github.com/seattlerb/ruby_parser
or
https://github.com/whitequark/parser
and will receive an AST (Abstrax Syntax Tree) which you then can process further. Depending on the amount of details you need from the source, you could also use some Regexps or write your own parser...
Perhaps you can tell us a little more about your project (input, output, reasons)

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)

.sample on an array is always false in irb [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 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

How to refactor following unless statement in Ruby [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
While learning Ruby, I got stucked with unless statement. Doing a full unless statement is sometimes too much. Please refactor the method below to use a single-line unless statement
games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
unless games.empty?
puts "Games in your vast collection: #{games.count}"
end
This code doesn't changes the unless statement to be inline, Please help me to modify this code so that it may work, Thanks.
use then
games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
puts "Games in your vast collection: #{games.count}" unless games.empty?
# >> Games in your vast collection: 4

Resources