1. परिचय
इस कोडलैब में, टेस्ट ड्रिवन डेवलपमेंट ( टीडीडी) के सिद्धांतों के बारे में बताया गया है. इसमें कोड लागू करने से पहले टेस्ट लिखे जाते हैं. हम इस तरीके को दिखाने के लिए, Ruby का इस्तेमाल करके एक सामान्य कैलकुलेटर ऐप्लिकेशन बनाएंगे.
आपको कोडिंग असिस्टेंट के तौर पर Gemini को इस्तेमाल करने का तरीका भी बताया जाएगा. Gemini, टेस्ट केस जनरेट करके, कोड लागू करने के सुझाव देकर, और कोड के बारे में जानकारी देकर, टीडीडी साइकल को तेज़ करने में मदद करेगा.
आखिर में, आपको कैलकुलेटर ऐप्लिकेशन को Cloud Functions पर डिप्लॉय करने का तरीका बताया जाएगा. Cloud Functions, Google का पूरी तरह से मैनेज किया गया सर्वरलेस प्लैटफ़ॉर्म है. इससे आपका ऐप्लिकेशन ऐक्सेस किया जा सकेगा.
2. ज़रूरी शर्तें
- बिलिंग की सुविधा वाला Google Cloud प्रोजेक्ट
- वेब ब्राउज़र
अपना प्रोजेक्ट बनाना
- Google Cloud Console में, प्रोजेक्ट चुनने वाले पेज पर जाकर, Google Cloud प्रोजेक्ट चुनें या बनाएं.
- पक्का करें कि आपके Cloud प्रोजेक्ट के लिए बिलिंग की सुविधा चालू हो. किसी प्रोजेक्ट के लिए बिलिंग चालू है या नहीं, यह देखने का तरीका जानें.
Gemini API को चालू करना
- एपीआई चालू करने के लिए, Gemini Marketplace पर जाएं. एपीआई चालू करने के लिए, gcloud का इस्तेमाल भी किया जा सकता है:
gcloud services enable cloudaicompanion.googleapis.com --project PROJECT_ID
- Gemini पेज पर जाएं और "चैटिंग शुरू करें" पर क्लिक करें.
- अहम जानकारी: Gemini का इस्तेमाल शुरू करने और Cloud Shell IDE में Gemini को चालू करने के लिए, इस कोड लैब में दिए गए पहले और दूसरे चरण को पूरा करें.
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
- यह पुष्टि करने के लिए कि gcloud कमांड को आपके प्रोजेक्ट के बारे में पता है, Cloud Shell में यह कमांड चलाएं:
gcloud config list project
- अगर आपका प्रोजेक्ट सेट नहीं है, तो इसे सेट करने के लिए इस निर्देश का इस्तेमाल करें:
gcloud config set project <YOUR_PROJECT_ID>
gcloud कमांड और उनके इस्तेमाल के बारे में जानने के लिए, दस्तावेज़ देखें.
एडिटर खोलें
- इस कोडलैब के लिए, हम Cloud में पहले से मौजूद एडिटर का इस्तेमाल करेंगे. क्लाउड शेल विंडो में, सबसे ऊपर दाएं कोने में मौजूद 'एडिटर खोलें' बटन पर क्लिक करें. इससे आपके लिए VSCode Editor खुल जाएगा.

Ruby एक्सटेंशन जोड़ना
- बाईं ओर मौजूद मेन्यू में, एक्सटेंशन आइकॉन
पर क्लिक करें. इसके बाद, खोज बार में vscode rubyलिखें और VSCode Ruby एक्सटेंशन इंस्टॉल करें.

3. Gemini का अभ्यास मैच
एलएलएम, नॉन-डिटरमिनिस्टिक होते हैं. इसलिए, इन प्रॉम्प्ट को आज़माते समय, आपको मिलने वाला जवाब, कोडलैब में अटैच किए गए स्क्रीनशॉट में मौजूद जवाबों से अलग दिख सकता है.
सबसे पहले, Gemini से पूछते हैं कि टीडीडी क्या है. Gemini से उसी Cloud Shell Editor में बातचीत शुरू करने के लिए, यह तरीका अपनाएं:
- पेज के बाईं ओर, 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 से पूछते हैं कि रूबी क्या है:
प्रॉम्प्ट:
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>/के नीचे फ़ोल्डर बनाएं

- आइए, अभी-अभी बनाए गए फ़ोल्डर को खोलें. सबसे बाईं ओर मौजूद मेन्यू में,
आइकॉन पर क्लिक करें. इसके बाद, फ़ाइल -> फ़ोल्डर खोलें पर क्लिक करें. - आइए, इस रूट फ़ोल्डर में एक नई 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. .....
यह आपको Calculator ऐप्लिकेशन के लिए, टेस्ट केस का एक सैंपल देता है. जैसे, दो संख्याओं को जोड़ने के लिए. इसने टेस्ट के लिए ज़रूरी डिपेंडेंसी 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 पर राइट क्लिक करें. इसके बाद, "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 को साफ़ तौर पर बताते हैं कि हमने app.rb में Calculator क्लास को तय किया है .
प्रॉम्प्ट:
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 क्लास को तय किया गया है. कभी-कभी सही जवाब पाने के लिए, आपको प्रॉम्प्ट को दो से तीन बार दोहराना पड़ सकता है. हालांकि, जब आपको इसकी आदत पड़ जाएगी, तो यह बेहतर तरीके से काम करेगा. कृपया ध्यान दें कि आपको Gemini के जवाब में, require "./app" भी दिख सकता है. इसे app_test.rb में app.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 से Calculator के ज़्यादा फ़ंक्शन मिल सकते हैं. हालांकि, हमने अभी जो तरीका बताया है वह काम करना चाहिए.
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 कमांड देने के लिए कहें, ताकि मैं इसे इंटिग्रेट किए गए टर्मिनल से ही चला सकूं.
प्रॉम्प्ट:
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
आइए, टर्मिनल में Gemini की दी गई gcloud कमांड को लागू करते हैं.
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. Ruby के लिए Functions Framework
आइए, 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 gem मौजूद नहीं है. चलिए, इसे अपने Gemfile में जोड़ते हैं:
gem "functions_framework"
अब टर्मिनल में bundle install चलाएं. आपको "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"
इस समस्या को हल करने के लिए, आइए Gemini की Generate Code सुविधा का इस्तेमाल करें. आइए, एक्सप्लोरर टैब से app.rb खोलें. इसमें पहले से ही कैलकुलेटर ऐप्लिकेशन का कोड मौजूद है. 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
We just created the entry point for the Cloud Functions app and we also learnt how to use Gemini's code suggestion in the editor. अब, ऐप्लिकेशन को फिर से डिप्लॉय करने की कोशिश करें.
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app --project PROJECT_ID
अगर सब ठीक रहता है, तो डिप्लॉयमेंट पूरा हो जाएगा. इसके बाद, आपको वह यूआरएल दिखेगा जहां आपका ऐप्लिकेशन होस्ट किया गया है. ठीक है, अब हम 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. एंड-टू-एंड टेस्टिंग
यूआरएल खोलें और क्वेरी पैरामीटर a और b को नीचे दिखाए गए तरीके से पास करें:
https://[APP-URL]/?a=2&b=3
आपको जवाब इस तरह दिखेगा : "Result: 5". कैलकुलेटर कई तरह की कार्रवाइयां करता है. इस ऐप्लिकेशन को इन कामों के लिए भी इस्तेमाल किया जा सकता है. साथ ही, Gemini से इन कामों में मदद ली जा सकती है.
10. नतीजा
हमने आज Gemini की मदद से, टीडीडी अप्रोच का इस्तेमाल करके Ruby ऐप्लिकेशन डेवलप करने का तरीका देखा. आपने देखा होगा कि Gemini आपको अलग-अलग नतीजे दे सकता है. हालांकि, यह उस भाषा में डेवलपमेंट की प्रोसेस को तेज़ करने में आपकी मदद कर सकता है जिसके बारे में आपको ज़्यादा जानकारी नहीं है. साथ ही, यह आपको फ़ंक्शन या स्निपेट दिखा सकता है, ताकि आप तेज़ी से सीख सकें.
कोई भी डेवलपर ( चाहे उसके पास कितना भी अनुभव हो), डेवलपमेंट प्रोसेस के लिए Gemini से मदद ले सकता है. अगर आपको कोड डेवलप नहीं करना है, तो Gemini का इस्तेमाल मौजूदा कोड को समझने के लिए किया जा सकता है. साथ ही, इससे मौजूदा कोड के लिए टेस्ट जनरेट करने के लिए कहा जा सकता है. मेरे लिए, यह सबसे बोरिंग काम है 🙂
Gemini के साथ कोडिंग का आनंद लें!