2012-05-04 4 views
11

में उपज और वापसी का उपयोग कोई भी मुझे रूबी में उपज और वापसी के उपयोग को जानने में मदद कर सकता है। मैं रूबी शुरुआत कर रहा हूं, इसलिए सरल उदाहरणों की अत्यधिक सराहना की जाती है।रूबी

अग्रिम धन्यवाद!

उत्तर

28

रिटर्न स्टेटमेंट उसी तरह काम करता है जो यह अन्य समान प्रोग्रामिंग भाषाओं पर काम करता है, यह उस विधि से आता है जिस पर इसका उपयोग किया जाता है। आप कॉल को वापस करने के लिए छोड़ सकते हैं, क्योंकि रूबी में सभी विधियां हमेशा अंतिम कथन लौटाती हैं। तो अगर आप इस तरह विधि खोज सकते हैं:

def method 
    "hey there" 
end 

वास्तव में की तरह कुछ कर रही के रूप में ही है कि:

def method 
    return "hey there" 
end 

yield दूसरे हाथ पर, विधि के लिए एक पैरामीटर के रूप में दिया ब्लॉक excecutes। तो अगर आप इस तरह की एक विधि है कर सकते हैं:

def method 
    puts "do somthing..." 
    yield 
end 

और फिर इस तरह इसका इस्तेमाल:

method do 
    puts "doing something" 
end 

कि का परिणाम है, स्क्रीन पर मुद्रण किया जाएगा निम्नलिखित 2 लाइनों:

"do somthing..." 
"doing something" 

आशा है कि इसे थोड़ा सा साफ़ कर दें। ब्लॉक पर अधिक जानकारी के लिए, आप this link देख सकते हैं।

+0

ब्लॉक का लिंक टूटा हुआ है – adamscott

+1

यहां एक अच्छा है [लिंक] (http://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes) – adamscott

+0

धन्यवाद @adamscott, मैंने आपके साथ लिंक बदल दिया है। चियर्स – Deleteman

6

yield विधि से जुड़े ब्लॉक को कॉल करने के लिए उपयोग किया जाता है। वर्तमान पद्धति से

[1, 2, 3].each {|elem| puts elem} 

return बाहर निकलता है, और वापसी मान के रूप में अपनी "कथन" का उपयोग करता है,: आप विधि और उसके मानदंड के बाद ब्लॉक (मूल रूप से सिर्फ घुंघराले ब्रेसिज़ में कोड) डालकर कर, इसलिए की तरह जैसे इतना:

def hello 
    return :hello if some_test 
    puts "If it some_test returns false, then this message will be printed." 
end 

लेकिन ध्यान दें कि आप नहीं किसी भी तरीकों में वापसी कीवर्ड का उपयोग करने के है; रूबी अंतिम बयान वापस कर देगा अगर इसका कोई रिटर्न नहीं मिलता है। इस प्रकार इन दोनों बराबर होते हैं:

# A simple iterator that operates on an array 
def each_in(ary) 
    i = 0 
    until i >= ary.size 
    # Calls the block associated with this method and sends the arguments as block parameters. 
    # Automatically raises LocalJumpError if there is no block, so to make it safe, you can use block_given? 
    yield(ary[i]) 
    i += 1 
    end 
end 

# Reverses an array 
result = []  # This block is "tied" to the method 
       #       | | | 
       #       v v v 
each_in([:duck, :duck, :duck, :GOOSE]) {|elem| result.insert(0, elem)} 
result # => [:GOOSE, :duck, :duck, :duck] 

और return, जो मैं अगर एक नंबर happy है देखने के लिए एक विधि को लागू करने का प्रयोग करेंगे के लिए एक उदाहरण:

def explicit_return 
    # ... 
    return true 
end 

def implicit_return 
    # ... 
    true 
end 

यहाँ yield के लिए एक उदाहरण है :

class Numeric 
    # Not the real meat of the program 
    def sum_of_squares 
    (to_s.split("").collect {|s| s.to_i ** 2}).inject(0) {|sum, i| sum + i} 
    end 

    def happy?(cache=[]) 
    # If the number reaches 1, then it is happy. 
    return true if self == 1 
    # Can't be happy because we're starting to loop 
    return false if cache.include?(self) 
    # Ask the next number if it's happy, with self added to the list of seen numbers 
    # You don't actually need the return (it works without it); I just add it for symmetry 
    return sum_of_squares.happy?(cache << self) 
    end 
end 

24.happy? # => false 
19.happy? # => true 
2.happy? # => false 
1.happy? # => true 
# ... and so on ... 

आशा है कि इससे मदद मिलती है! :)

+0

उत्तर के लिए धन्यवाद :) – nxhoaf

+0

हाँ, कोई समस्या नहीं!:) – Jwosty

+0

@Jwosty इसे 'def खुश नहीं होना चाहिए? (कैश = []) '? –

0
def cool 
    return yield 
end 

p cool {"yes!"} 

उपज कीवर्ड ब्लॉक में कोड निष्पादित करने के लिए रूबी को निर्देश देता है। इस उदाहरण में, ब्लॉक स्ट्रिंग "yes!" देता है। cool() विधि में एक स्पष्ट वापसी विवरण का उपयोग किया गया था, लेकिन यह भी अंतर्निहित हो सकता था।