DevHeads.net

Postings by Michael Schwarze

CSV::Table: #delete_if vs. #each_with_index + #delete behaving differently in one use case?!

Hi!

I’ve got a CSV::Table object from which I’d like to delete rows containing fields with nil values. The following approach does work but does not allow to keep track of the deleted records:

csv_data.delete_if { |row| row[:a].nil? or row[:b].nil? or row[:c].nil? }

Hence I came up with the following approach:

csv_data.each_with_index do |row, index|
if row[:a].nil? or row[:b].nil? or row[:c].nil?
LOGGER.info("Record nbr.

VS Code for Ruby – Experience?!

Hi,

Has anybody experience using Visual Studio Code for Ruby?

I haven’t got a special problem and I dont’t want to start another IDE war, but I’m looking for some real life experience with this IDE and Ruby; is somebody using VS Code for developing Ruby applications? Any pros/cons re. this setup? Something a starter with this setup should now about?

I’m on Mac using the latest Ruby and VS Code installations with the »standard« Ruby extension rebornix.Ruby and necessary dependecies like ruby-debug-ide, debase installed.

Many thanks!

Regards,
Michael

'Uninitialized' attr_reader at class returning name of class instead of nil

Hi,

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this case?

Many thanks for any hints!

Regards,
Michael

Creating a struct from an array of symbols

Hello!

I’d like to create a Struct[1] from an array containing symbols representing the attributes of the Struct but can’t get it to work:

~~~
arr = [:attr1, :attr2, :attr3]

s = Struct.new(arr)
# => TypeError: no implicit conversion of Array into String
~~~

I’ve tried different conversions (to_s, etc.) and OpenStruct[2], too.

Optimizing iterating over an array

Hi!

I’ve got a method which checks an array of arrays containing CSV data coming from CSV::read and returns the (corrected) array:

~~~
def check_data(csv_data)
...
csv_data.each { |row| LOGGER.info("Record not processed due to empty field(s): #{row}") if row[:field1].nil? }
csv_data.delete_if { |row| row[:field1].nil? }
end
~~~

It works but I don’t like the last two statements iterating two times over the whole array for logging and deleting rows where the first field contains nil values.

How to populate a two-dimensional array with objects "the Ruby way"?

Hi,

I need to populate a two-dimensional array with (individual) objects. I came up with the following approach:

~~~
class Test
end

tests = Array.new(3) { Array.new(3) { Test.new }}

#=> [[#<Test:0x007f871a059918>, #<Test:0x007f871a0598f0>, #<Test:0x007f871a0598c8>], [#<Test:0x007f871a059878>, #<Test:0x007f871a059850>, #<Test:0x007f871a059828>], [#<Test:0x007f871a0597d8>, #<Test:0x007f871a0597b0>, #<Test:0x007f871a059788>]]

~~~

It works, but looks kind of »ugly« to me, esp. the nested curly braces.

Warning: string literal in condition – how to best check whether a value is within a range

Hi,

I’d like to implement a guard clause to check whether a value is within a certain range but it gives me a warning: »string literal in condition«:

~~~
def billable?(activity)
return false if activity[:project_nbr] == '900'..'999'
# …
true
end
~~~

I’ve looked this up ([1], [2], [3]) and can follow the examples given there and why

~~~
if input == "N" || "n"
~~~

has to be

~~~
if input == "N“ || or input "n"
~~~

But I can’t really get my head around what’s wrong with my above mentioned code.

Anyway, I’ve changed the guard clause to

~~~
return false if activity[:project_n

Command-query separation and explicit vs. implicit return in Ruby

Hi,

I’ve been wondering for some time about how to best handle command-query separation (CQS) and explicit vs.

Best practice for structuring of code: splitting up and arranging of methods

Hi,

I’m curious for some insight or best practice regarding how to best structure Ruby code. I sometimes split larger methods up into smaller ones and end up with something similar to this:

~~~
def method
m1
m2
end

def m1
m1_1
m1_2
end

def m2
m2_1
m2_2
end
~~~

Is there anything 'bad‘ from a coding style point of view (code smell or anti pattern) about this? If not, how would an experienced Ruby developer expect the method definitions to be arranged within the source code?

Splitting up of a certain method into two not working (for me)

Hi,

I’ve got a problem with splitting up my below stated method into two.

Difference between a method »living« in a module (as namespace) and in main

Hi,

I’m struggling to get my head around the difference between a method being within main and the same method »living« in a module.

The following Ruby script is a simplified example of where it worked and started for me:
—-
def tst
puts "Tst: in #{inspect}"
end
puts "Main: in #{inspect}"
tst

#=> Main: in main
#=> Tst: in main
—-

After wrapping this script into a module as a namespace to organize the code it returns a NameError:
—-
module M
def tst
puts "Tst: in #{inspect}"
end
puts "Main: in #{inspect}"
tst
end

#=> Main: in M
#=> undefined local va

Rubocop: Question re. 'Use a guard clause instead of wrapping the code inside a conditional expression.'

Hello,

Rubocop complains with the following message with one of my methods:

C: Use a guard clause instead of wrapping the code inside a conditional expression.
if value >= 0
^^
#—-
def get_credit_debit(value)
if value >= 0
return 'C'
else
return 'D'
end
end
#—-

This approach, although maybe being not very elegant, is from my point of view at least expressive / tells the reader exactly what’s going on.

I found the following advice [1] re.

Method on module level not working

Hi,

I’ve written a little ruby application with roughly 20 methods in two classes which I have kind of wrapped in one module as a common namespace to hold everything together. I introduced optparse on the module level to parse options which worked fine.