1. مقدمة
يقدّم لك هذا الدرس التطبيقي حول الترميز مبادئ التطوير المستند إلى الاختبار ( TDD)، حيث تكتب الاختبارات قبل تنفيذ الرمز. سننشئ تطبيق آلة حاسبة بسيطًا باستخدام Ruby لعرض هذا الأسلوب.
ستتعرّف أيضًا على كيفية استخدام Gemini كمساعدك في البرمجة. سيساعد Gemini في تسريع دورة تطوير البرامج المستندة إلى الاختبارات من خلال إنشاء حالات اختبار واقتراح عمليات تنفيذ للرموز البرمجية وتقديم تفسيرات لها.
أخيرًا، ستتعرّف على كيفية تفعيل تطبيق الآلة الحاسبة على Cloud Function، وهي منصة Google للحوسبة بدون خادم المُدارة بالكامل، ما يتيح تسهيل استخدام تطبيقك.
2. المتطلبات الأساسية
- مشروع Google Cloud تم تفعيل الفوترة فيه
- متصفّح ويب
إنشاء مشروعك
- في Google Cloud Console، في صفحة اختيار المشروع، اختَر أو أنشِئ مشروعًا على Google Cloud.
- تأكَّد من تفعيل الفوترة في مشروعك على السحابة الإلكترونية. تعرَّف على كيفية التحقّق مما إذا كانت الفوترة مفعَّلة في مشروع.
تفعيل Gemini API
- انتقِل إلى Gemini Marketplace لتفعيل واجهة برمجة التطبيقات. يمكنك أيضًا استخدام gcloud لتفعيل واجهة برمجة التطبيقات:
gcloud services enable cloudaicompanion.googleapis.com --project PROJECT_ID
- انتقِل إلى صفحة Gemini وانقر على "بدء محادثة".
- ملاحظة مهمة: اتّبِع الخطوتَين 1 و2 في درس الترميز التطبيقي هذا للبدء في استخدام Gemini وتفعيل Gemini في بيئة تطوير متكاملة (IDE) في Cloud Shell على التوالي.
تفعيل Cloud Functions API
- في Google Cloud Console، في صفحة اختيار المشروع، اختَر أو أنشِئ مشروعًا على Google Cloud.
- انتقِل إلى صفحة Cloud Functions API لتفعيل واجهة برمجة التطبيقات. يمكنك أيضًا استخدام gcloud لتفعيل واجهة برمجة التطبيقات:
gcloud services enable cloudfunctions.googleapis.com --project PROJECT_ID
تفعيل Cloud Shell
- ستستخدم Cloud Shell، وهي بيئة سطر أوامر تعمل في Google Cloud Console ومحمَّلة مسبقًا باللغات المطلوبة. من Cloud Console، انقر على تفعيل Cloud Shell في أعلى يسار الصفحة:

- بعد الاتصال بـ Cloud Shell، من المفترض أن يظهر لك أنّه تم إثبات هويتك وأنّ المشروع مضبوط على رقم تعريف مشروعك. نفِّذ الأمر التالي في Cloud Shell للتأكّد من إثبات هويتك:
gcloud auth list
- نفِّذ الأمر التالي في Cloud Shell للتأكّد من أنّ أمر gcloud يعرف مشروعك:
gcloud config list project
- إذا لم يتم ضبط مشروعك، استخدِم الأمر التالي لضبطه:
gcloud config set project <YOUR_PROJECT_ID>
راجِع المستندات لمعرفة أوامر gcloud وطريقة استخدامها.
فتح "المحرّر"
- في هذا الدرس التطبيقي، سنستخدم محرّر Cloud المضمّن. في نافذة Cloud Shell، انقر على الزر "فتح المحرّر" (Open editor) في أعلى يسار الصفحة. سيؤدي ذلك إلى فتح محرِّر VSCode.

إضافة Ruby Extension
- في قائمة الجانب الأيمن، انقر على رمز الإضافات
، واكتب vscode rubyفي شريط البحث، ثم ثبِّت إضافة VSCode Ruby.

3- تمارين الإحماء في Gemini
النماذج اللغوية الكبيرة غير قطعية. لذا، أثناء تجربة هذه الطلبات، قد يختلف الردّ الذي تتلقّاه عن الردود الواردة في لقطات الشاشة المرفقة في دليل التدريب العملي.
لنبدأ أولاً بسؤال Gemini عن تعريف TDD. لبدء محادثة مع Gemini في "محرّر Cloud Shell" نفسه الذي نستخدمه، اتّبِع الخطوات التالية:
- على يمين الصفحة، ابحث عن رمز Gemini
، وسيؤدي ذلك إلى فتح نافذة المحادثة على يمين الصفحة. - يمكنك البدء بالكتابة في مربّع النص الذي يظهر فيه
Enter a prompt here

الطلب:
What is Test Driven Development?
الردّ:
Test-driven development (TDD) is a software development methodology that requires developers to write automated tests before writing any production code. This helps ensure the code is correct and meets the requirement. ....
تقدّم لك هذه المقالة مقدمة موجزة عن عملية التطوير المستند إلى الاختبار ومزاياها.
لنطرح الآن على Gemini السؤال التالي: ما هي لغة Ruby؟
الطلب:
What is Ruby?
الردّ:
Ruby is a general-purpose, interpreted, object-oriented programming language. It is designed to be easy to read and write. Ruby is dynamically typed, meaning that the type of a variable is not declared and can change during execution. ....
توضّح هذه المقالة ماهية Ruby ومزايا استخدامها كلغة برمجة.
4. إعداد المشروع
لنبدأ الآن بإنشاء تطبيق Sinatra بسيط.
- لننشئ مجلدًا فارغًا. يمكنك اختيار أي تسمية تريدها. سنُطلق عليه اسم
gemini-tddفي هذا الدرس التطبيقي حول الترميز. في "محرّر السحابة الإلكترونية" على الجانب الأيمن ضمن علامة التبويب "المستكشف"، أنشئ مجلدًا ضمن/home/<YOUR-USERNAME>/

- لنفتح المجلد الذي أنشأناه للتو. في القائمة الموجودة في أقصى اليمين، انقر على الرمز
، ثم انقر على "ملف" (File) -> "فتح مجلد" (Open Folder). - لننشئ ملف Ruby جديدًا في مجلد الجذر هذا، وسيكون هذا الملف بمثابة نقطة بداية للتطبيق، ويمكنك تسميته كما تريد. سنطلق عليه اسم
app.rbفي هذا الدرس التطبيقي حول الترميز.
- لنضِف أيضًا
Gemfileفي المجلد الجذر. من الجيد أيضًا أن تسأل Gemini عنGemfile. - سننشئ أيضًا ملف
app_test.rbلكتابة حالات الاختبار.
5- التنفيذ المستند إلى Gemini
لنبدأ الآن بتزويد Gemini ببعض السياق حول ما نحاول فعله ونطلب منه إنشاء حالات اختبار لذلك. انسخ الطلب التالي في طلب محادثة Gemini.
الطلب:
Answer as a software engineer with Ruby expertise. We want to build a Ruby calculator application with a test driven development approach. Let's start by writing some tests for the calculator app using minitest gem.
الردّ:
Here is a simple test for a calculator app that adds two numbers:
require "minitest/autorun"
describe "Calculator" do
it "should add two numbers" do
calculator = Calculator.new
assert_equal 5, calculator.add(2, 3)
end
end
This test uses the assert_equal method to verify that the result of the add method is correct. .....
يقدّم لك حالة اختبار نموذجية لتطبيق "الآلة الحاسبة"، أي لإضافة رقمَين. تم أيضًا استيراد التبعية المطلوبة minitest/autorun للاختبارات. لننسخ المحتوى إلى app_test.rb. لنحاول تشغيل هذا الاختبار الآن، وبما أنّه ليس لدينا أي رمز مكتوب، سيتعذّر إجراء الاختبار بالتأكيد.
لنسأل Gemini عن كيفية إجراء هذا الاختبار.
الطلب:
How do we run the tests written in app_test.rb using ruby command?
الردّ:
To run the tests written in app_test.rb using the ruby command, you can use the following steps: 1.Make sure that you are in the same directory as the app_test.rb file. 2.Open a terminal window. 3.Type the following command:
ruby app_test.rb
This will run all of the tests in the app_test.rb file and report the results.
لنبدأ إذًا في إجراء الاختبار. انقر بزر الماوس الأيمن على app_test.rb في علامة التبويب "المستكشف" (Explorer) وانقر على "فتح في الوحدة الطرفية المدمجة" (Open in Integrated Terminal).

تأكَّد من أنّك في المجلد نفسه الذي يحتوي على الملف app_test.rb، وإذا لم يكن الأمر كذلك، استخدِم الأمر cd للانتقال إلى هناك. في الوحدة الطرفية، شغِّل الأمر الذي يعرضه Gemini:
ruby app_test.rb
سيظهر لك الخطأ التالي:
NameError: uninitialized constant Calculator
لنطرح على Gemini سؤالاً حول كيفية حلّ هذه المشكلة مباشرةً من المحرّر. انقر على رمز المحادثة في لوحة التنقّل في محرّر Cloud Shell واطرح الطلب أدناه:
الطلب:
NameError: uninitialized constant Calculator. Give me Calculator class code.
الردّ:
The error you are getting is because you have not defined the Calculator class. You need to define the Calculator class before you can use it in your tests. Here is the code for the Calculator class:
Class Calculator
def add(a, b)
a + b
end
end
Once you have defined the Calculator class, you can run the tests again and they will pass. .....
يخبرنا Gemini أنّه ليس لدينا فئة باسم Calculator وأنّنا بحاجة إلى إنشاء واحدة. وقد قدّمت لنا أيضًا الرمز البرمجي لإنشاء الفئة Calculator. لنستخدم هذا الرمز. افتح app.rb من علامة التبويب "المستكشف" وانسخ الرمز الذي قدّمه Gemini فيه. أثناء تعديل المحتوى في ملف app.rb، قد تتلقّى بعض الاقتراحات من Gemini كإكمال تلقائي. عليك الضغط على tab إذا أردت قبول الاقتراح أو على Esc إذا لم ترد قبوله.
لنُجري الاختبار مرة أخرى. اكتب الأمر التالي في الوحدة الطرفية المدمجة:
ruby app_test.rb
ومع ذلك، سيظلّ الخطأ نفسه "NameError: uninitialized constant Calculator" يظهر لك. لنطرح على Gemini سؤالاً حول كيفية إصلاحه.
6. التطوير المستند إلى الاختبار بشكل متكرّر باستخدام Gemini
الطلب:
I am still getting this error: "NameError: uninitialized constant Calculator".
الردّ:
The error you are getting is because you have not defined the Calculator class. You need to define the Calculator class before you can use it in your tests. Here is the code for the Calculator class:
class Calculator
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
def multiply(a, b)
a * b
end
def divide(a, b)
a / b
end
end
....
لقد قدّم لنا بعض الردود، وقد تكون مماثلة لما ورد أعلاه أو لا. لذا، لنخبر Gemini صراحةً بأنّ لدينا فئة Calculator معرَّفة في app.rb .
الطلب:
Calculator class is defined in app.rb already.
الردّ:
I see that the Calculator class is defined in the app.rb file. However, you are getting the NameError error because you are trying to use the Calculator class in the app_test.rb file without requiring the app.rb file. To fix this, you need to add the following line to the top of the app_test.rb file:
require_relative "app"
لقد حصلنا على الردّ من Gemini لاستيراد ملف app.rb الذي تم فيه تحديد فئة Calculator. في بعض الأحيان، قد يستغرق الأمر 2 أو 3 تكرارات للطلبات للحصول على الردّ المناسب، ولكن مع التدريب، سيتحسّن الأداء. يُرجى العِلم أنّه قد يظهر لك أيضًا require "./app" بدلاً من ذلك، وذلك في الردّ من Gemini. لنضِف هذا إلى app.rb في app_test.rb وننفِّذ الاختبار نفسه مرة أخرى.
يُرجى التأكّد من أنّ ملف app_test.rb يبدو على النحو التالي في هذه المرحلة:
require "minitest/autorun"
require "./app"
describe "Calculator" do
it "should add two numbers" do
calculator = Calculator.new
assert_equal 5, calculator.add(2, 3)
end
end
نفِّذ الأمر التالي من "وحدة Cloud Shell الطرفية" ضِمن مجلد المشروع:
ruby app_test.rb
إذا سارت الأمور على ما يرام، من المفترض أن يجتاز الاختبار. لذلك، طوّرنا تطبيق Ruby بسيطًا جدًا باستخدام أسلوب التطوير المستند إلى الاختبار، واستعنّا بـ Gemini. لن تكون الردود على الطلبات هي نفسها في كل مرة، وهذا من طبيعة نماذج اللغات الكبيرة. قد تحصل على المزيد من عمليات الآلة الحاسبة من Gemini، ولكن بشكل عام، يجب أن تنجح العملية نفسها التي اتّبعناها للتو.
7. نشر التطبيق في Cloud Functions
لنطلب الآن من Gemini نشر هذا الرمز البرمجي في Cloud Function:
الطلب:
Let's deploy this app to Cloud Functions
الردّ:
To deploy your app to Cloud Functions, you can use the following steps: 1.Create a new Cloud Functions project. 2.Create a new Cloud Function. 3.Set the trigger for the Cloud Function to be an HTTP request. 4.Set the entry point for the Cloud Function to be the app method in the app.rb file. 5.Deploy the Cloud Function. Once you have deployed the Cloud Function, you can test it by sending an HTTP request to the function's URL. ....
توضّح Gemini كيفية نشرها يدويًا باستخدام واجهة مستخدم Cloud Functions. لكنّني لا أريد فعل ذلك. لنطلب من Gemini أن يقدّم لنا الأمر gcloud الذي يمكنني تنفيذه مباشرةً من نافذة Terminal المدمجة.
الطلب:
How do I deploy this app to Cloud Functions gen2 using the gcloud command?
الردّ:
...
Here are the specific commands you can use to deploy your app to Cloud Functions using the gcloud command:
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
This command will create a new Cloud Function called calculator that is triggered by an HTTP request and uses the app method in the app.rb file as the entry point.
Once the Cloud Function is deployed, you can test it by sending an HTTP request to the function's URL. For example, you can use the following command to send a request to the function:
curl -X POST -H "Content-Type: application/json" -d '{"a": 2, "b": 3}' https://REGION-PROJECT_ID.cloudfunctions.net/calculator
لننفِّذ أمر gcloud الذي قدّمه Gemini في الوحدة الطرفية.
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
سيظهر لك الخطأ التالي:
"ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Could not find Gemfile.lock file in your app. Please make sure your bundle is up to date before deploying.; Error ID: 7916f05c"
لنطلب المساعدة من Gemini لإصلاح الخطأ.
الطلب:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Could not find Gemfile.lock file in your app. Please make sure your bundle is up to date before deploying.; Error ID: 7916f05c
الردّ:
It looks like you're trying to deploy a function to Cloud Functions, but you don't have a Gemfile.lock file in your app. To fix this, run the following command in your app directory:
bundle install
This will create a Gemfile.lock file that lists all of the gems that your app uses. ...
لننفّذ الأمر bundle install في الوحدة الطرفية، ما سيؤدي إلى إنشاء Gemfile.lock . بعد تنفيذ الأمر، انتقِل إلى علامة التبويب "المستكشف"، من المفترض أن تتمكّن من رؤية Gemfile.lock .
لنحاول نشر التطبيق على Cloud Functions مرة أخرى. نفِّذ أمر النشر نفسه في الوحدة الطرفية:
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
سيظهر لك الخطأ التالي:
"ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: unable to execute functions-framework-ruby; please ensure a recent version of the functions_framework gem is in your Gemfile; Error ID: 89ae7dda"
8. Functions Framework for Ruby
لنطرح على Gemini السؤال التالي حول الخطأ:
الطلب:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: unable to execute functions-framework-ruby; please ensure a recent version of the functions_framework gem is in your Gemfile; Error ID: 89ae7dda
الردّ:
It looks like you're trying to deploy a function to Cloud Functions, but the build is failing because the functions_framework gem is not in your Gemfile. To fix this, add the following line to your Gemfile: gem "functions_framework" Then, run the following command to install the gem: bundle install Once the gem is installed, you can try deploying your function again. ....
لذا، ينقصنا في تطبيقنا حزمة functions_framework. لنضِف هذا السطر إلى ملف Gemfile:
gem "functions_framework"
الآن، شغِّل bundle install في الوحدة الطرفية. قد يظهر لك الخطأ "No explicit global resource in Gemfile" أو لا يظهر. إذا واجهت هذه المشكلة، تأكَّد من أنّ السطر الأول في Gemfile هو:
source "https://rubygems.org"
بعد ذلك، شغِّل bundle install من نافذة الوحدة الطرفية مرة أخرى. الآن، لننفِّذ أمر النشر مرة أخرى:
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app --project PROJECT_ID
سيظهر لك الخطأ التالي الآن:
"ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: failed to verify function target "app" in source "app.rb": Undefined function: "app"; Error ID: e12ab0f9"
لحلّ هذه المشكلة، لنستخدِم ميزة Generate Code في Gemini. لنفتح الملف app.rb من علامة التبويب "المستكشف" (Explorer)، حيث يتوفّر لدينا رمز تطبيق "الآلة الحاسبة" (Calculator). في app.rb، في السطر الأول، ابدأ بكتابة FunctionsFramework، ثم اكتب ".". ستظهر لك بعض الاقتراحات بنص رمادي :

اضغط على مفتاح التبويب (Tab) لقبول الاقتراح. بعد قبول ذلك، ستظهر لك المزيد من الاقتراحات كما هو موضّح أدناه:

اضغط على مفتاح التبويب (Tab) لقبول المزيد من الاقتراحات. لذلك، يجب أن يبدو ملف app.rb على النحو التالي:
( إعادة تسمية اسم الدالة من add إلى app )
require "functions_framework"
FunctionsFramework.http "app" do |request|
a = request.params["a"].to_i
b = request.params["b"].to_i
Calculator.new.add(a, b)
end
class Calculator
def add(a, b)
a + b
end
end
لقد أنشأنا للتو نقطة الدخول إلى تطبيق Cloud Functions، وتعلّمنا أيضًا كيفية استخدام اقتراحات الرموز البرمجية من Gemini في أداة التعديل. الآن، لنحاول نشر التطبيق مرة أخرى.
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app --project PROJECT_ID
وإذا سارت الأمور على ما يرام، من المفترض أن يتم نشر التطبيق وأن تتمكّن من الاطّلاع على عنوان URL الذي تتم استضافة تطبيقك عليه. حسنًا، لنعدّل ملف app.rb قليلاً لعرض نتيجة الجمع. يجب أن يبدو ملف app.rb النهائي على النحو التالي:
require "functions_framework"
FunctionsFramework.http "app" do |request|
a = request.params["a"].to_i
b = request.params["b"].to_i
calculator = Calculator.new
add = calculator.add(a, b)
"Result: #{add}"
end
class Calculator
def add(a, b)
a + b
end
end
9- الاختبار الشامل
افتح عنوان URL وأدخِل مَعلمات طلب البحث a وb كما هو موضّح أدناه:
https://[APP-URL]/?a=2&b=3
من المفترض أن يظهر الرد على النحو التالي : "Result: 5". تتوفّر العديد من العمليات التي يمكن للحاسبة تنفيذها. يمكنك توسيع نطاق هذا التطبيق ليشمل هذه المهام أيضًا والاستفادة من مساعدة Gemini في ذلك.
10. الخاتمة
لقد رأينا اليوم كيفية تطوير تطبيق Ruby باستخدام أسلوب التطوير المستند إلى الاختبار بمساعدة Gemini. كما لاحظت، قد يقدّم لك Gemini نتائج مختلفة، ولكن يمكنه بالتأكيد المساعدة في تسريع عملية التطوير بلغة قد لا تكون على دراية كبيرة بها، وسيظل يعرض لك وظائف أو مقتطفات لتسريع منحنى التعلّم.
يمكن لأي مطوّر ( بغض النظر عن مستوى خبرته) الاستعانة بـ Gemini في عملية التطوير. إذا لم يكن هدفك التطوير، يمكنك استخدام Gemini لشرح الرموز البرمجية الحالية، ويمكنك أن تطلب منه إنشاء اختبارات للرموز البرمجية الحالية، وهو ما أراه على الأقل من أكثر المهام مللاً 🙂
نتمنّى لك تجربة ترميز ممتعة مع Gemini.