samedi 12 juin 2021

How to Get Both Coordinates In a Nested Array?

I'm working on a chess game and want to search the nested array for the King piece K(b)and then return the coordinates.

I've tried the following method which returns 4 (the x coordinate - the index in the internal array). However, I'm trying to figure out how to also return index of the outer array. Expected output is [4, 7]:

piece = "K(b)"

def coordinates_for_piece(piece)
  @board.map { |item| item.find_index(piece) }
end

Board:

def piece_setup
    @board = Array.new(8) { Array.new([" .  ", " .  ", " .  ", " .  ", " .  ", " .  ", " .  ", " .  "]) }

    @board[0] = ["R(w)", "N(w)", "B(w)", "Q(w)", "K(w)", "B(w)", "N(w)", "R(w)"]
    @board[1] = ["P(w)", "P(w)", "P(w)", "P(w)", "P(w)", "P(w)", "P(w)", "P(w)"]
   
    @board[6] = ["P(b)", "P(b)", "P(b)", "P(b)", "P(b)", "P(b)", "P(b)", "P(b)"]
    @board[7] = ["R(b)", "N(b)", "B(b)", "Q(b)", "K(b)", "B(b)", "N(b)", "R(b)"]
end

EDIT: This is a VERY clunky way of accomplishing the goal. Looking for something more streamlined.

def coordinates_for_piece(piece)
   test = @board.map { |item| item.find_index(piece) }
   => [nil, nil, nil, nil, nil, nil, nil, 4]
   x = test.find{|x| !x.nil?}
   => 4
   y = test.find_index{|y| !y.nil?}
   => 7
   [x, y]
end

jeudi 10 juin 2021

Create a image with MiniMagick::Image getting improper image header

I'm creating a png image with MiniMagick::Image and it giving me an false result when I checked with valid? function: there are 2 error composite: ImproperImageHeader and composite: MissingAnImageFilename

Here is my code:

img = MiniMagick::Image.new(Tempfile.new(['label_creation_date', '.png']).path)

MiniMagick::Image.new(Tempfile.new(['label_creation_date', '.png']).path).tap do |tmp_image|
  MiniMagick::Tool::Convert.new.tap do |convert|
    convert << tmp_image.path
    convert.background 'transparent'
    convert.rotate '-4'
    convert << tmp_image.path.to_s
  end
end

I tried one more solution that in gives in https://github.com/minimagick/minimagick/issues/59#issuecomment-3775000 here, but unfortunately it's also not working.

Please help me how can I create a valid image from scratch with the help of minimagick?

Thanks

mardi 8 juin 2021

rails_admin gem auto sorting when stored as an array

I am using rails_admin gem

I am saving data in the form of array . The data is being stored.

I am facing an issue while editing the data. While editing and saving the data the array is auto sorting. I haven't written any sort array function.

How can I stop the auto sortarray while saving

Thank you

dimanche 6 juin 2021

Error: port 22: Resource temporarily unavailable in wsl

I am using capistrano gem to deploy my rails app on ec2 instance and i am facing Error: port 22: Resource temporarily unavailable in wsl.

I have tried the following things but facing the same problem still:

  1. eval ssh-agent -s
  2. Added inbound and outbound rule

I am not able to figure out the problem now

samedi 5 juin 2021

Improving a query. One book for each author

I need an hand to improve this query

def interesting_books
  Book.joins(:genre)
    .where(author: interesting_authors.sample(5))
    .where('ratings >= 4')
    .random_order
    .where.not(id: black_books)
    .limit(3)
end

def interesting_authors
 @interesting_authors ||= (authors_commented_books + authors_watched_books).uniq
end

def authors_commented_books
 @authors_commented_books ||= current_user.commented_books.pluck(:author).uniq
end

def authors_watched_books
 @authors_watched_books ||= current_user.watched_books.pluck(:author).uniq
end

now, in this way if in the .where(author: interesting_authors.sample(5)) i have authors like "Shakespear", "Twain" , "Fitzgerald", "Wilde" and "Darwin"... it shows only 3 books (.limit(3)) made by "Shakespear". Result: "Macbeth", "Othello" and "The Tempest".

BUT I want to show 3 books made by different (selected) authors. Like "Othello", "The Great Gatsby" and "Dorian Gray".

How to do?

vendredi 4 juin 2021

bundle install is failed on windows for ruby

Please Im trying to run bundle install but its not finished and I got that error: city.cc:91:10: fatal error: byteswap.h: No such file or directory, it said Make sure that `gem install cityhash -v '0.9.0', i did that but i got the same error saying it can't install cityhash. enter image description here

How to extract from an array of hash

people = [
    {id: 101, first_name: 'John', last_name: 'Doe'},
    {id: 102, first_name: 'Tom', last_name: 'Rogers'},
    {id: 103, first_name: 'Bill', last_name: ''}
]

I want to put a list of name like this, "John Doe, Tom Rogers, Bill"

How can i achieve this is in ruby?