Ruby Syntactic Sugar: Dealing with Nils

ruby syntactic sugar: dealing with nils

For the first I'd recommend ick's maybe (equivalent to andand)

"a string".match(/abc(.+)abc/).maybe[1]

I am not sure I understand the second one, you want this?

var = something.very.long.and.tedious.to.write || something.other

Use ruby syntactic sugar to print different strings depending on the format of the string

How about:

output_string = "a hate james blunt this much : "
output_string += how_much.is_numeris? ? "'#{string}'" : string

Is there a shorthand for assigning from $1...$n after matching in ruby

If you use match instead of =~, you get back a MatchData object, which has a method captures, which returns the values matched by the capturing groups in an array:

first, second, third = var.match(/.../).captures

Nils and method chaining

I think you can find a great solution in rails but that solution follows a different approach. Take a look at the try method. It's a clean approach.

Protecting against an undefined chained method

The issue is that a product is nil:

undefined method 'page_url' for nil:NilClass". Solution:

(It has nothing to do with page_url maybe returning nil.)

Make sure product can't be nil: but be wary that this may be a deeper issue. In any case, "fixing" this issue is easy to deal with.

Consider either using a collection restriction (such as Enumerable#reject):

@all_products.reject(&:nil?).each do {
...
}

The above uses the Symbol#to_proc "Rails magic", but could just as easily have been {|x| x.nil?} as the restriction. The downside is it's not practical to use this for a "no URL" condition per-product although Enumerable#partition could help with that: use the right tool for the job.

Another solution is to expand the conditional check itself:

if product && product.page_url("en_US")
# yay
else
# uhm
end

The short-circuit nature of && will ensure page_url is only invoked upon a truthy value (which excludes nil).

I also took the liberty of assuming page_url can't return false as I find this makes the intent more clear.

Happy coding.

Is there a clean way to avoid calling a method on nil in a nested params hash?

Check Ick's maybe. You don't need to significantly refactor your code, just intersperse maybe proxies when necessary:

params[:subject].maybe[:name]

The same author (raganwald) also wrote andand, with the same idea.

Elegant way to merge 2 text files alphabetically line by line in Ruby

Sometimes adding a dimension makes a solution prettier. Essentially, turn your file1, file2 variables into an array [ file1, file2 ], and this opens up a lot of Ruby Array syntax that performs the tests you have coded into your initial solution.

if ARGV.length < 2
puts "Wrong number of arguments. Expected 2 or more files to merge."
end

merged_file = File.open("merge_out.txt", "w")

files = ARGV.map { |filename| File.open( filename, "r") }

lines = files.map { |file| file.gets }

while lines.any?
next_line = lines.compact.min
file_id = lines.index( next_line )
merged_file.print next_line
lines[ file_id ] = files[ file_id ].gets
end

So not only is this shorter, but as a side effect can handle more input files at once. Although if you don't need that, simply change back first check.



Related Topics



Leave a reply



Submit