Can You Use Semicolons in Ruby

Can you use semicolons in Ruby?

Yes.

Ruby doesn't require us to use any character to separate commands, unless we want to chain multiple statements together on a single line. In this case, a semicolon (;) is used as the separator.

Source: http://articles.sitepoint.com/article/learn-ruby-on-rails/2

Semicolon after class declaration

Explain, please, why is it possible to use semicolon here and what does ';' means in these context

Semicolons are optional in ruby. This means that using them is actually valid syntax. Having a newline is enough for the parser to separate statements. In the case you mention the semicolon is required as there is not a newline as a statement separator.

The following are all valid options:

class Dog
end

class Dog;
end

class Dog; end

The following is invalid (with ruby >= 2.3 this seems to be fine though)

class Dog end

For class or method definitions with an empty body using one line with a semicolon separator is idiomatic in ruby. If you have a body you'll usually omit the semicolon and place the content on a new line.

UPD: I thought, that

Java's syntax parser doesn't allow as much flexibility as ruby's parser. A class or method definition in Java is strict syntax which requires a newline after the class name and before the definition body.

semicolon as statement separator in Rails Console

You need to say "user." so Ruby knows you mean to call the attribute assignment methods of the instance of user. Otherwise, you are just setting local variables called "hashed_password", etc.

>> user.username = "John"; user.hashed_password = "John"; user.first_name = "John"; user.last_name = "coltrane"; user.email = "John@coltrane.com"; user.display_name = "Johndispay"; user.user_level = 9; 

Although, you could just pass a hash of the attributes you want to set on the new instance, like so

>> user = User.new(:username => "John", :hashed_password => "John", ...

vim auto indenting lines without semicolons in Ruby

You probably don't want set cindent on ruby files.

Use au FileType ruby setlocal nocindent

At the top (before any other au's) for other languages to work properly you need

instead of their respective lines.

Rails GraphQL: Parameter of resolver-methods - Meaning of the semicolon-suffix?

The syntax is defining a keyword argument that the caller has to explicitly state:

def v(id:)
p id
end

v(1) # error

v(id: 1)
1
=> 1

v(something: 1)
ArgumentError (missing keyword: id)

It has its uses, primarily for interaction in the console and ensuring the user knows what they are doing before they call the method, or ensuring new developers of the codebase understand how to call the method.

If the required argument is missing, Ruby throws a nice ArgumentError which is concise and reduces the cognitive overhead for future developers to engage with a codebase.

Capistrano/Ruby execute is adding a semicolon after a variable

It is because you have newlines in your command, which Capistrano is translating into semicolons, thinking you want to execute multiple commands (one per line).

Backticks in Ruby capture the entire stdout of the process, including trailing newlines. Use chomp to remove them.

git_message = `git log -1 HEAD --pretty=format:%s`.chomp
git_commit = `git rev-parse HEAD`.chomp
git_commit = "https://example.com/myorg/myrepo/commit/" + git_commit

execute "echo \"Deployed \\\"#{git_message}\\\": #{git_commit}\" | a_command_that_posts_to_slack"

How to unescape in query_string double semicolons?

I think you will have to encode your params first to

/posts?from=45&q=mad%3B%3Bdepot&size=15

Then Rails will recognize it correctly.

For more information about encode URL: http://www.w3schools.com/tags/ref_urlencode.asp



Related Topics



Leave a reply



Submit