Panayotis Matsinopoulos

Senior Software Engineer: Love Creating Software, Taking Entrepreneurial Risks, Teaching Computer Programming & Blogging here or in many other places


My Courses


My Contact Details


My Projects


My Scheduled Classes


Books

Neo4j Essentials:

I have been technical reviewer on this book. That was a very exciting experience. Click below to see details of the book.


Posts

Ruby Array Sum

| Comments

This short post is going to demonstrate how we can sum an array of integers or an array of objects that have an integer attribute.

Let’s start:

Sum array of integers

First version using reduce with block

1
2
3
4
5
array_of_i = [10, 2, 8, 5, 7, 9]

sum = array_of_i.reduce(0) { |result, item| result + item }

puts "Sum of #{array_of_i.join(",")} is: #{sum}"

Second version using reduce giving the method symbol to apply

1
2
3
4
5
array_of_i = [10, 2, 8, 5, 7, 9]

sum = array_of_i.reduce(:+)

puts "Sum of #{array_of_i.join(",")} is: #{sum}"

Sum of objects that have an integer attribute

Using reduce with a block

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Product
  attr_accessor :price

  def initialize(price)
    self.price = price
  end
end

prod1 = Product.new(1000)
prod2 = Product.new(500)
prod3 = Product.new(1500)

array_of_products = [prod1, prod2, prod3]

sum = array_of_products.reduce(0) {|result, item| result + item.price}

puts "Sum of array_of_products is: #{sum}"

I am pretty sure that you can come up with other methods of doing the above. Can you post in the comments any alternatives? Advantages and disadvantages?

Class vs Instance Variables

| Comments

demo of `Class` vs `Instance` variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Here, I am defining a class Dog with an class variable "@@number_of_feet"
# and an instance variable "@color", assuming that all dogs in the world
# have the same number of feet but they might differ in color. Or, at least,
# this is the general rule.
#
class Dog
  @@number_of_feet = 4

  def initialize(color)
    @color = color
  end

  def number_of_feet=(value)
    @@number_of_feet = value
  end

  def tell_me_about_you
    puts "My color is #{@color} and I have #{@@number_of_feet} feet"
  end
end

# The next two statements will set the "@color" instance variable to different values
# for max and rocky. But both dogs will have the same number of feet, which will be 4,
# since when class is initialized the "@@number_of_feet" instance variable takes the value 4.

max = Dog.new('black')
rocky = Dog.new('brown')

max.tell_me_about_you    # will print black and 4
rocky.tell_me_about_you  # will print brown and 4

# Now, I am changing the value of the class variable "@@number_of_feet" and I am setting that
# to "3". You will see that now all dogs change to have 3 feet. 
max.number_of_feet = 3

max.tell_me_about_you    # will print black and 3
rocky.tell_me_about_you  # will print brown and 3

United States, Counties, Cities, Zips CSV & Ruby Loading Code

| Comments

I just wanted to have the United States, their Counties, the Cities in the Counties and the Zip codes in each City in CSV format. But I wanted that normalized. In different CSVs. So, here it is. You can also find here some Ruby on Rails sample code that loads these files. Primitive stuff, but sometimes useful.

My sponsor on that is Book&Table
I am about to start a Seminar: Introduction to Programming. If you are interested in, register here.

The Clean Coder - a Code of Conduct for Professional Programmers - R. Martin (Pearson)

| Comments

I have recently finished the book “The Clean Coder – A Code of Conduct for Professional Programmers – R. Martin (Pearson, 2011) BBS”. Here is my short review:

Overall: Fantastic. Worth reading for every developer that wants to become professional.

My Notes and Highlights

Chapter 1 – Professionalism

  • Professionalism is a marker of responsibility and accountability.
  • It is better to make your manager unhappy rather than your manager customers.

Twitter Bootstrap 2 Modal Scrolling Problem | Why Capybara/selenium Cannot Locate Your Visible Buttons

| Comments

Twitter Bootstrap 2 has a bug while on modal dialogs. The page does not scroll as expected. So, if the browser window is not wide/tall enough to display the whole modal dialog content, the scroll bars do not make it appear.

The bug is described here:

And the answer is given a little bit below that:

This problem does not occur with jQuery modals neither with Twitter Bootstrap 3.

I am using Ruby on Rails and the less-rails-bootstrap gem.

In order to fix the problem for my current application, and instead of moving to Twitter Bootstrap 3 or to jQuery, I have decided to fix the problem in the gem

Here is the fix:

gem 'less-rails-bootstrap', :git => 'https://github.com/pmatsinopoulos/less-rails-bootstrap.git', :branch => 'bug-fix-modal-scrolling'

Important Note This bug influences your capybara/selenium tests, because elements that are normally visible, while the browser is maximized, they cannot be located by selenium and they are reported as non-visible and cannot be interacted with, if the window browser is not open enough to display the whole modal dialog.

Model Properties - Styling

| Comments

I usually come across people asking what is the preferred way of laying out the properties of a model and, in fact, of an ActiveRecord model. Shall we put first the validations and then the associations? Or first the associations and then the validations? Or shall we put first the callbacks?

One can google for rails style guide. Will basically find only this:

which is a good resource. But is not complete.

My Rails Model Style Guidelines

we are talking here about Rails 3

Here is the list of my styling guidelines and which I use whenever I am writing a Model.

Including vs Extending a Module

| Comments

Demonstration of `include` vs `extend`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Here, I am defining a "module" with name Ma. I also define two methods. One without "self."
# and one with. See, later on, what happens when I "include" and what happens when I "extend"
# the "module" within a "class".
#
module Ma
  # I will be able to make this method an instance or a class method of a class.
  # It depends whether I will "include" or "extend" the module in the class.
  # Note that this method, I cannot call it directly on Ma. In order for this method
  # to be useful, I have to include or extend this module within a class.
  #
  def ma_method1
    puts "ma_method1"
  end

  # This method, is not reusable, in the sense that I cannot make it be an instance or class
  # method of a class. But still, it is a method of module Ma and I can call it directly.
  #
  def self.ma_method2
    puts "ma_method2"
  end
end

puts "Ma responds to ma_method1? : #{Ma.respond_to?(:ma_method1)}" # it will print "false"
puts "Ma responds to ma_method2? : #{Ma.respond_to?(:ma_method2)}" # it will print "true"
puts "-------------"

class A
  # "include" sets the module methods as instance methods of the class
  include Ma
end

a = A.new
puts "a Responds to ma_method1?: #{a.respond_to?(:ma_method1)}" # it will print "true"
puts "A Responds to ma_method1?: #{A.respond_to?(:ma_method1)}" # it will print "false"
puts "a Responds to ma_method2?: #{a.respond_to?(:ma_method2)}" # it will print "false"
puts "A Responds to ma_method2?: #{A.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------"

class B
  # "extend" sets the module methods as class methods of the class
  extend Ma
end

b = B.new
puts "b Responds to ma_method1? : #{b.respond_to?(:ma_method1)}" # it will print "false"
puts "B Responds to ma_method1? : #{B.respond_to?(:ma_method1)}" # it will print "true"
puts "b responds to ma_method2? : #{b.respond_to?(:ma_method2)}"  # it will print "false"
puts "B responds to ma_method2? : #{B.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------------"

But you can also include or extend a module in another module too. Read this gist by pglombardo who has kindly appended to the above piece of code.