Void value expression error?

Hi,

I am very new to Ruby, I wrote the below I am not getting where I am
doing mistake but I am getting Void value expression error… Can
anybody suggest me please where I am doing mistake…

def myFun(response)
rows = Array.new
details = Array.new
i = 0
rowsCount = countOccurrences("$", response)
rows = parseString(response, “|$”, rowsCount)

flag = true
detailsCount = countOccurrences("|",rows[i])

while detailsCount > i
details = parseString(rows[i], “|”, detailsCount)

if flag
myMulArr = Array.new(rowsCount) { Array.new(detailsCount) }
flag = false
end

j = 0
while j < detailsCount
myMulArr[i][j] = details[j]
j = j+1
end
i++

if i == rowsCount then
break
end

end
return myMulArr
end

Hi,

You seem to be programming a kind of “Java in Ruby” rather than actual
Ruby.

There is no “i++” in Ruby. “camelCase” identifiers and “Array.new” also
aren’t used in Ruby.

Hi Jan,

Thanks. Your guess is right. I am from java technology. I made a mistake
at i+…

Jan E. wrote in post #1078368:

Hi,

You seem to be programming a kind of “Java in Ruby” rather than actual
Ruby.

There is no “i++” in Ruby. “camelCase” identifiers and “Array.new” also
aren’t used in Ruby.

Ruby is actually very different from Java, regarding both the syntax and
the general approach. Java has a C-like syntax and is pretty low level
with a lot of classical procedural programming (increment this counter,
decrement that one etc.). Ruby, on the other hand, has a syntax roughly
similar to Pascal and is very high level. Instead of juggling with flags
and counters, you can use methods known from functional programming like
“select”, “reject”, “map”, “zip” etc., which will make your code much
more readable and compact.

So when you program with Ruby, it might be a good idea to forget
everything you know from Java and actually use the features Ruby is
offering. If I understand you correctly, you have a kind of CSV with
rows ending with “$” and columns ending with “|” and want to make a 2
dimensional array out of it. In Ruby, this is as simple as

test_csv = ‘a|b|c|$d|e|f|$’
table = test_csv.split(’$’).map {|row| row.split(’|’)}
p table

(To be fair, this will get a bit longer if you also want to take empty
fields into account)

There are also specialized CSV libraries that might be more appropriate
than reading everything into an array.

But even if you want to stick to your approach, you could still make it
much shorter just by omitting the Java-like boilerplate code and using
built-in methods. You don’t need to initialize arrays with a certain
length, and you don’t need to define your own methods for splitting
strings or counting substrings.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs