Simple Ruby 'or' question
["hello", "goodbye"].include? @user.user_type
Simple ruby syntax question
Here is the exact code that you are using under the hood:
https://github.com/marcel/aws-s3/blob/master/lib/aws/s3/bucket.rb
As you can see, there are nested modules/classes:
module AWS
module S3
class Bucket < Base
end
end
end
So:
- AWS is a module.
- S3 is a module.
- Bucket is a class.
The class Bucket is nested inside the module S3 which is nested inside the module AWS.
A Module is basically a bundle of methods/constants, but they differ from classes in the sense where they can't have instances. You use that a lot in order to refactor your code and to better design it. More information on Modules here.
The :: is used to refer to the nested modules/classes. It's a kind of resolution operator, that helps you reach your nested modules/classes/constants by knowing their paths.
Basic ruby question: the { } vs do/end construct for blocks
In the "not working" case, the block is actually attached to the puts
call, not to the collect
call. {}
binds more tightly than do
.
The explicit brackets below demonstrate the difference in the way Ruby interprets the above statements:
puts(test = [1, 1, 1].collect) do |te|
te + 10
end
puts test = ([1, 1, 1].collect {|te|
te + 10
})
Ruby question mark operator, what does this mean?
(true ? rand(13) : 0)
mean(if true then rand(13) else 0 end)
if you have directly "true" in condition, the "else" is never called (is useless), you can write : a = 1 + rand(13)
directly ;)
rand(13)
give random int between 0 and 12 ;)
if you want "13" putrand(14)
personally I always use range like this (all range is include, it's easier to understand) :rand(0..13)
Newbie Ruby question - project basic calculator
Use if statement with elsif
and else
.
math = gets.chomp.downcase
if math == "add"
puts user + user2
elsif math == "subtract"
puts user - user2
elsif math == "multiply"
puts user * user2
else
puts "I don't understand"
end
case
is also nice for this usecase.
math = gets.chomp.downcase
case math
when "add"
puts user + user2
when "subtract"
puts user - user2
when "multiply"
puts user * user2
else
puts "I don't understand"
end
Ruby and Rails Simple Question on Expression
In Ruby, if a variable does not have a value, it is nil
by default. To answer your second question, you can use the send
method to call your "type" methods. Something like,
def get_profession(type)
# Making sure correct type is passed.
raise RuntimeError unless ['first', 'second', 'third'].include?(type.to_s)
send(type)
end
Send expects a method in a string as parameter, which it calls on the same object. In your case if the response of your "type" method has no value, it would be by default be nil
.
Related Topics
Error in Celluloid Gem Installation
Save All Image Files from a Website
How to Access the Current Node from a Library in a Chef Cookbook
How to Upload a File with Watir and Ie
Adding a Background Image in Ruby on Rails 2 in CSS
Sass Variables Not Parsing Correctly - Undefined Variable: "$Ct-White"
How to Create Dynamic CSS in Rails
Ruby on Rails - Link_To Button/Css
Using SASS with User-Specified Colors
How to Set a Variable from a Helper Method for Inclusion in a SASS SCSS Stylesheet
Using Phonegap as a Native Container for a Rails 3 App